#include <iostream>
#include <algorithm>
using namespace std;
struct mice{
    int weight;
    string color;
};
bool cmp(mice x,mice y)
{
    return x.weight>y.weight;
}
int main() {
    int n;
    mice a[100];
    while (cin >> n) { 
        for(int i=0;i<n;i++)
        {
            cin>>a[i].weight>>a[i].color;
        }
        sort(a,a+n,cmp);
        for(int i=0;i<n;i++)
        {
            cout<<a[i].color<<endl;
        }
    }
}