#include<bits/stdc++.h>
using namespace std;
struct st{
string a;
int b;
};
bool cmp(st x,st y)
{
if(x.b!=y.b)
return x.b>y.b;
else
return y.a>x.a;
}
int main()
{
st x[110];
int n;
while(cin>>n)
{
for(int i=0;i<n;i++)
cin>>x[i].a>>x[i].b;
sort(x,x+n,cmp);
for(int i=0;i<n;i++)
cout<<x[i].a<<" "<<x[i].b<<endl;
}
return 0;
}