#include <bits/stdc++.h>
using namespace std;
struct per
{
int num;
int age;
string id;
} p[5050];
bool cmp(per a, per b)
{
if (a.age < 60 && b.age < 60)
return a.num < b.num;
if (a.age == b.age)
return a.num < b.num;
return a.age > b.age;
}
int n;
int main()
{
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> p[i].id >> p[i].age;
p[i].num = i;
}
sort(p + 1, p + n + 1, cmp);
for (int i = 1; i <= n; i++)
{
cout << p[i].id << endl;
}
return 0;
}