#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    while(cin>>n)
    {
        int id[100],score[100];
        for(int i = 0;i<n;i++)
        {
            cin>>id[i]>>score[i];
        }

        for(int i = 0;i<n-1;i++)
        {
            for(int j = 0;j<n-i-1;j++)
            {
                if(score[j]>score[j+1]||(score[j] == score[j+1]&&id[j]>id[j+1]))
                {
                    int temp1 = score[j];
                    score[j] = score[j+1];
                    score[j+1] = temp1;
                    int temp2 = id[j];
                    id[j] = id[j+1];
                    id[j+1] = temp2;
                }
            }
        }
        for(int i = 0;i<n;i++)
        {
            cout<<id[i]<<" "<<score[i]<<endl;
        }
    }

    return 0;
}