//土尔逊Torson 编写于2023/4/10
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <algorithm>
#include <stdlib.h>

using namespace std;

struct Student {
	int num;
	int grade;
};

bool comp(Student left, Student right) {
	if (left.grade < right.grade) {
		return true;
	}
	else if (left.grade == right.grade && left.num < right.num) {
		return true;
	}
	else {
		return false;
	}
}

int main() {
	Student arr[100];
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; ++i)
	{
		scanf("%d%d", &arr[i].num, &arr[i].grade);
	}
	sort(arr, arr + n, comp);

	for (int j = 0; j < n; ++j)
	{
		printf("%d %d\n", arr[j].num, arr[j].grade);
	}

	system("pause");
	return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")