#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>

using namespace std;

const int MAXSIZE = 100;
int arr[MAXSIZE];

struct Anim{
    int weigh;
    string color;
};

Anim anim[MAXSIZE];

bool Compare(Anim x,Anim y){
    return x.weigh>y.weigh;
}

int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0; i<n; ++i)
        {
            cin>>anim[i].weigh>>anim[i].color;
//             scanf("%d",&arr[i]);
        }

        sort(anim,anim+n,Compare);

        for(int i=0; i<n; ++i)
        {
//             printf("%d ",arr[i]);
           cout<<anim[i].color<<endl;
        }
    }
}