#include<iostream>
#include<string>
using namespace std;
struct patient{
int age;
char id[10];
int sequence;
};
int Compare(patient a,patient b)
{
if(a.age>=60&&b.age>=60){
if(a.age<b.age){
return true;
}
else if(a.age=b.age){
if(a.sequence>b.sequence){
return true;
}
}
}
if(a.age<60&&b.age<60){
if(a.sequence>b.sequence){
return true;
}
}
return false;
}
void PatientResult(patient *s,int n)
{
for(int i=0;i<n;i++)
{
cout<<s[i].id<<endl;
}
}
void PatientSort(patient *s,int num)
{
patient tmp;
for(int i=0;i<num-1;i++){
for(int j=0;j<i;j++){
if(Compare(s[j],s[j+1])){
tmp=s[j];
s[j]=s[j+1];
s[j+1]=tmp;
}
}
}
}
int main()
{
int n;
patient s[100];
cin>>n;
for(int i=0;i<n;i++)
{
s[i].sequence=i;
cin>>s[i].id>>s[i].age;
}
PatientSort(s,n);
PatientResult(s,n);
return 0;
}