#include <stdio.h>
int main(){
    int number;
    scanf("%d", &number);
    //定义一个student结构体,包含学号和成绩
    struct student{
        int num;
        int grade;
    }student[number];
    //输入学生信息
    for (int i = 0; i < number; ++i) {
        scanf("%d %d", &student[i].num, &student[i].grade);
    }
    //冒泡排序
    struct student temp;
    for (int i = 0; i < number-1; ++i) {
        for (int j = 0; j < number - 1 - i; ++j) {
            if((student[j].grade > student[j+1].grade)
            || ((student[j].grade == student[j+1].grade) && student[j].num > student[j+1].num)){
                temp = student[j];
                student[j] = student[j+1];
                student[j+1] = temp;
            }
        }
    }
    //输出
    for (int i = 0; i < number; ++i) {
        printf("%d %d\n", student[i].num, student[i].grade);
    }
    return 0;
}