#include<cstdio>
#include<algorithm>

using namespace std;

struct mice {
    int weight;
    char colour[100];
};

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