#include<bits/stdc++.h>
using namespace std;
struct stu{
string s;
int sum;
}bcb[600];
int cmp(stu x,stu y){
if(x.sum<y.sum)return 1;
return 0;
}
int main(){
int n,y,m,d;
cin>>n;
for(int i=1;i<=n;i++){
cin>>bcb[i].s>>y>>m>>d;
bcb[i].sum=y*365+m*30+d;
}
sort(bcb+1,bcb+n+1,cmp);
for(int i=1;i<=n;i++)cout<<bcb[i].s<<endl;
return 0;
}