#include
#include<bits/stdc++.h>
#include
using namespace std;
bool mycompare(pair<string,int> x,pair<string,int> y)
{
if(x.second==y.second)
{
return x.first<y.first;
}
return x.second>y.second;
}
int main() {
int x;
cin>>x;
pair<string,int> employee;
int n;
cin>>n;
string name;
int salary;
for(int i=0;i<n;i++)
{
cin>>name;
cin>>salary;
employee[i].first=name;
employee[i].second=salary;
}
sort(employee,employee+n,mycompare);
for(int i=0;i<n;i++)
{
cout<<employee[i].first<<" "<<employee[i].second<<endl;
}
return 0;
}