bool mycompare(pair<string,int> a1,pair<string,int> a2)
{
if(a1.second==a2.second)
return a1.first < a2.first;
return a1.second > a2.second;
}
int main()
{
int min_sal,n;
cin>>min_sal;
cin>>n;
pair<string,int> emp[100005];
string name;
int sal;
for(int i=0;i<n;++i)
{
cin>>name>>sal;
emp[i].first=name;
emp[i].second=sal;
}
sort(emp,emp+n,mycompare);
return 0;
}