#include<bits/stdc++.h>
using namespace std;
struct xqw{
string name;
int C , m , E ;
}a[301];
bool cyy(xqw x , xqw y){
if(x.C < y.C) return 1;
else if(x.C < y.C) return 0;
else{
if(x.m < y.m) return 1;
else if(x.m > y.m) return 0;
else{
if(x.E <= y.E) return 1;
else if(x.E > y.E) return 0;
}
}
}
int main(){
int n;
cin >> n;
for(int i = 0 ; i < n ; i ++)
cin >> a[i].name >> a[i].C >> a[i].m >> a[i].E;
sort(a , a + n , cyy);
for(int i = 0 ; i < n ; i++)
cout << a[i].name << endl;
return 0;
}