#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
struct mouse{
    int weight;
    string color;
};
bool comp(mouse x,mouse y){
    return x.weight>y.weight;
}

int maxn=100;
int main(){
    int arr[maxn];
    int n;
    while(scanf("%d",&n)!=EOF){
        mouse arr[maxn];
        for(int i=0;i<n;i++){
            cin>>arr[i].weight>>arr[i].color;
        }
        sort(arr,arr+n,comp);
        for(int i=0;i<n;i++){
            cout<<arr[i].color<<endl;
        }
    }
    return 0;
}