#include<stdio.h>
typedef  struct d {
    char a[20];
    int sore;

} book;
void sort(book* s, int n) {
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n - 1 - i; j++) {
            if (s[j].sore > s[j + 1].sore) {
                book temp = s[j];
                s[j] = s[j + 1];
                s[j + 1] = temp;
            }
        }
    }


}
int main() {
    book s[100];
    int n;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        scanf("%s%d", s[i].a, &(s[i].sore));
    }
    sort(s, n);
    for (int i = 0; i < n; ++i) {
        printf("%s\n", s[i].a);
    }
    return 0;
}