#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
struct date
{
string name;
int sum;
}a[100];
int cmp(date x,date y)
{
if(x.sum!=y.sum)return x.sum<y.sum;
if(x.sum == y.sum)return x.sum>y.sum;
return 0;
}
int main()
{
int n;
cin>>n;
for(int i = 0;i<n;i++)
{
int year,month,day;
cin>>a[i].name>>year>>month>>day;
a[i].sum = year*365+month*30+day;
}
sort(a,a+n,cmp);
for(int i = 0;i<n;i++)
{
cout<<a[i].name<<endl;
}
return 0;
}