#include <stdio.h>
struct animal{
    int weight;
    char color[20];
};

void sort(struct animal a[],int n){//排序由大到小
    for(int i=0;i<n-1;i++){
        for(int j=0;j<n-i-1;j++){
            struct animal temp;
            if(a[j].weight<a[j+1].weight){
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
}

int main() {
    int n;
    while (scanf("%d", &n) != EOF) {
        struct animal a[n];
        for(int i=0;i<n;i++){
            scanf("%d %s",&a[i].weight,a[i].color);
        }
        sort(a,n);
        for(int j=0;j<n;j++){
            printf("%s\n",a[j].color);
        }
    }
    return 0;
}