#include <iostream>
using namespace std;
const int N = 10010;
typedef pair<string, int> PSI;
PSI q[N], tmp[N];
int f(int a, int b, int c){
return a * 2 + b * 3 + c;
}
bool cmp2(string a, string b){
int len = a.size();
for (int i = 0; i < len; i ++)
if (a[i] != b[i]) return a[i] < b[i];
return true;
}
bool cmp1(PSI a, PSI b){
if (a.second != b.second) return a.second > b.second;
return cmp2(a.first, b.first);
}
void merge_sort(PSI q[], int l, int r){
if (l >= r) return;
int mid = l + r >> 1;
merge_sort(q, l, mid), merge_sort(q, mid + 1, r);
int i = l, j = mid + 1, k = 0;
while (i <= mid && j <= r)
if (cmp1(q[i], q[j])) tmp[k ++] = q[i ++];
else tmp[k ++] = q[j ++];
while (i <= mid) tmp[k ++] = q[i ++];
while (j <= r) tmp[k ++] = q[j ++];
for (int i = l, j = 0; i <= r; i ++, j ++) q[i] = tmp[j];
}
int main(){
int n;
cin >> n;
for (int i = 1; i <= n; i ++){
int a, b, c;
cin >> q[i].first;
cin >> a >> b >> c;
q[i].second = f(a, b, c);
}
merge_sort(q, 1, n);
if (n > 2) n = 2;
for (int i = 1; i <= n; i ++)
cout << q[i].first << endl;
return 0;
}