#include <algorithm>
#include <iostream>
#include <cstdio>
using namespace std;

struct smallMouse{
    int weight;
    char color[20];
};

bool Comp(smallMouse a,smallMouse b){
    return a.weight > b.weight;
};

int main() {
    int N;
    smallMouse smouse[100];
    while(scanf("%d",&N) != EOF){
        for (int i = 0; i < N; ++i) {
            scanf("%d %s",&smouse[i].weight,smouse[i].color);
        }
        sort(smouse, smouse+N,Comp);
        for (int j = 0; j < N; ++j) {
            printf("%s\n",smouse[j].color);
        }
    }
}