#include<bits/stdc++.h>
using namespace std;
struct worker{
string name;
int e;
}w[10005];
int cmp(worker x , worker y){
return x.e > y.e;
}
int main(){
int n;
cin >> n;
for(int i = 1;i <= n;i++) cin >> w[i].name >> w[i].e;
sort(w+1,w+n+1,cmp);
for(int i = 1;i <= n;i++) cout << w[i].name << ' ' << w[i].e << '\n';
return 0;
}