#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define len 2000

typedef struct mos{
    int weight;
    char color[100];
}mos;

mos initial_mos(int weight,char color[]){
    mos ms;
    ms.weight = weight;
    strcpy(ms.color,color);
    return ms;
}

mos mos_list[len];

int cmp(const void*s1,const void*s2){
    mos a1 = *(mos*)s1;
    mos a2 = *(mos*)s2;
    return a2.weight-a1.weight;
}

void sort_list(int n){
    qsort(mos_list,n,sizeof(mos),cmp);
}

int main(){
    int n;
    while(scanf("%d",&n)!=EOF){
        int weight;
        char color[100];
        for(int i = 0;i<n;i++){
            scanf("%d %s",&weight,color);
            mos_list[i] = initial_mos(weight,color);
        }
        sort_list(n);
        for(int i = 0;i<n;i++){
            printf("%s\n",mos_list[i].color);
        }
    }
}