#include<bits/stdc++.h>
using namespace std;
int n,m,sum;
struct tongxue{
char name[20];
int y,m,d,t;
}p[5005];
bool poopsort(tongxue s1,tongxue s2){
if(s1.y!=s2.y) return s1.y<s2.y;
else if(s1.m!=s2.m) return s1.m<s2.m;
else if(s1.d!=s2.d) return s1.d<s2.d;
else return s1.t<s2.t;
}
int main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>p[i].name>>p[i].y>>p[i].m>>p[i].d;
p[i].t=i;
}
for(int i=n-1;i>0;i--){
for(int j=1;j<=i;j++){
if(!poopsort(p[j],p[j+1])){
swap(p[j],p[j+1]);
}
}
}
for(int i=1;i<=n;i++){
cout<<p[i].name<<endl;
}
return 0;
}