#include<bits/stdc++.h>
using namespace std;
struct st{
string s;
int y,m,d;
}a[1000000];
bool cmp(st c,st b){
if(c.y<b.y)return 1;
else if(c.y>b.y)return 0;
else {
if(c.m<b.m)return 1;
else if(c.m>b.m)return 0;
else {
if(c.d<b.d)return 1;
else if(c.d>b.d )return 0;
else if(c.d==b.d)return 1;
}
}
}
int main(){
int n;cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i].s>>a[i].y>>a[i].m>>a[i].d;
}
sort(a+1,a+1+n,cmp);
for(int i=1;i<=n;i++){
cout<<a[i].s<<endl;
}
return 0;
}