#include <bits/stdc++.h>
#define sz(x) (int) x.size()
#define pb push_back
#define all(x) x.begin(), x.end()
using namespace std;

int main() {
    int t;
    while(cin >> t) {
        vector<pair<int, string>> a(t);
        for(auto& [d, n] : a) cin >> n >> d;
        sort(all(a));

        for(int i=0; i<t; i++) {
            if(i > 0) cout << " ";
            cout << a[i].second;
        }
        cout << endl;
    }
}
