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