#include<cstdio>
#include<algorithm>

using namespace std;

struct Mouce{
    int weight;
    char color[100];
};

bool comp(Mouce lhs ,Mouce rhs){
    if(lhs.weight > rhs.weight){
        return true;
    }
    else{
        return false;
    }
}
int main(){
    int n;
    Mouce arr[101];
    while(scanf("%d",&n)!=EOF){
        for(int i = 0 ; i < n ;++i){
            scanf("%d %s\n",&arr[i].weight,arr[i].color);
        }
        sort(arr,arr+n,comp);
        for(int i = 0 ; i < n ; ++ i ){
        printf("%s\n",arr[i].color);
        }
    }
}