#include<stdio.h>
#include<stdlib.h>

typedef struct Student{
    int no;
    int score;
}Student;

int Compare(const void *a,const void *b){
    if(((Student*)a)->score==((Student*)b)->score)
        return ((Student*)a)->no-((Student*)b)->no;
    else
        return ((Student*)a)->score-((Student*)b)->score;
}

int main() {
    int n;
    while (scanf("%d",&n)!=EOF) { // 注意 while 处理多个 case
       Student s[n];
       for(int i=0;i<n;i++){
            scanf("%d%d",&s[i].no,&s[i].score);
       }
       qsort(s,n,sizeof(s[0]),Compare);
       for(int i=0;i<n;i++){
            printf("%d %d\n",s[i].no,s[i].score);
       }
    }
    return 0;
}