#include <bits/stdc++.h>
using namespace std;
const int N = 101001;
struct student
{
string name;
int num;
int y;
int m;
int d;
} stu[N];
bool cmp(student a, student 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 > a.m)
return false;
else
{
if (a.d < b.d)
return true;
else if (a.d > b.d)
return false;
else
{
if (a.num > b.num)
return true;
else
return false;
}
}
}
}
int main()
{
int n;
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> stu[i].name >> stu[i].y >> stu[i].m >> stu[i].d;
stu[i].num = i;
}
sort(stu, stu + n, cmp);
for (int i = 0; i < n; i++)
cout << stu[i].name << endl;
return 0;
}