#include <bits/stdc++.h>
using namespace std;
struct pe{
int y, m, d;
long long sum;
string s;
} per [110];
int n;
long long cmp(pe a , pe b) {
return a.sum < b.sum;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
cin >> n;
for(int i = 1 ; i <= n ; i ++) {
cin >> per[i].s >> per[i].y >> per[i].m >> per[i].d;
per[i].sum = per[i].y * 10000 + per[i].m * 100 + per[i].d * 1;
}
sort(per + 1 , per + n + 1 , cmp);
for(int i = 1 ; i <= n ; i ++) {
cout << per[i].s <<endl;
}
return 0;
}