#include <stdio.h>
#include <string.h>
#include <stdlib.h>

// write your code here......
struct Books{
    char** BooksName;
    int* BooksPrice;
}books;
void swap_int(int* a, int* b){
    int temp = *a;
    *a = *b;
    *b = temp;
}
void swap_char(char** a, char** b){
    char* temp = *a;
    *a = *b;
    *b = temp;
}
void up_out(char** book_name, int* book_price, int len){
    struct Books books = {book_name, book_price};
    for(int i=0; i<len-1; i++){
        for(int j=0; j<len-i-1; j++){
            if(book_price[j]>book_price[j+1]){
                swap_int(&book_price[j], &book_price[j+1]);
                swap_char(&book_name[j], &book_name[j+1]);
            }
        }
    }
    for(int i=0; i<len; i++){
        printf("%s\n", books.BooksName[i]);
    }
}
int main() {

    int n;
    scanf("%d",&n);

    char** book_name=(char**)malloc(n*sizeof(char*));
    int* book_price=(int*)malloc(n*sizeof(int));

    for (int i = 0; i < n; i++) {
        book_name[i]=(char*)malloc(100*sizeof(char));
        scanf("%s %d",book_name[i],&book_price[i]);
    }

    // write your code here......
    up_out(book_name, book_price, n);
    for (int i = 0; i < n; i++) {
        free(book_name[i]);
    }
    free(book_name);
    free(book_price);
    return 0;
}