#include <bits/stdc++.h>
using namespace std;

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	
	int n;
	while (cin >> n){
		map<string, int> mp;
		for (int i = 0; i < n; i++) {
			string mt;
			int val;
			cin >> mt >> val;
			mp[mt] = val;
		}
		vector<pair<string, int>> vt;
		for (auto it : mp)
			vt.push_back(it);
		sort(vt.begin(), vt.end(), [](pair<string, int> a, pair<string, int> b) {
			if (a.second != b.second)
				return a.second < b.second;
			return a.first < b.first;
		});
		for (int i = 0; i < n; i++)
			cout << vt[i].first << " \n"[i == n - 1];
	}
}