//用sort排序,构造比较函数(返回值为true时,参数1放到参数2的前面)
#include<iostream>
#include<algorithm>using namespace std;
struct student{
int number;
int grade;
};
student ss[101];
bool compare(student s1,student s2){ //构造比较函数
if(s1.grade==s2.grade)return s1.number<s2.number;
else return s1.grade<s2.grade;
}
int main(){
int n;
while(cin>>n){
for(int i=0;i<n;i++){
cin>>ss[i].number>>ss[i].grade;
}
sort(ss,ss+n,compare);
for(int i=0;i<n;i++){
cout<<ss[i].number<<" "<<ss[i].grade<<endl;
}
}
return 0;
}