#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool cmp(pair<int,int> a,pair<int,int> b){
    if(a.second==b.second){
        return a.first<b.first;
    }
    return a.second<b.second;
}
int main() {
    int n, score,id;
    cin>>n;
    vector<pair<int ,int >> vec;
    for(int i=0;i<n;i++){
        cin>>id>>score;
        vec.push_back(pair(id,score));
    }
    sort(vec.begin(),vec.end(),cmp);
    for(auto it : vec){
        cout<<it.first<<" "<<it.second<<endl;
    }
}
// 64 位输出请用 printf("%lld")