# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
using namespace std;
using LL = long long;
constexpr long long MOD = 1000000000 + 7;
constexpr long long INF = 1000000000;
const double PI = acos(-1);

int main() {
	int n;
	string s;
	int a, b;
	while (cin >> n&&n) {
		vector<pair<int, string>>v;
		for (int i = 0; i < n; i++) {
			cin >> s >> a >> b;
			v.emplace_back(make_pair(b, s));
		}
		sort(v.begin(), v.end());
		for (int i = n-1; i >= 0; i--) {
			cout << v[i].second << endl;
		}
	}
}