#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N;
    cin >> N;
    int p, q;
    vector<int> a(N + 1);
    for (int i = 0; i < N; i++) {
        cin >> p >> q;
        a[p] = q;
    }
    vector<int> b(N + 1);
    for (int i = 0; i <= N; i++) {
        for (int j = i + 1; j <= N; j++) {
            //b[i]越大,a[i]越小,a[i]越先输出
            int temp = a[i] <= a[j] ? i : j;
            b[temp]++;
        }
    }
    for (int i = 0; i <= N; i++) {
        if (a[i] == 0) b[i] = -1;
    }
    int max, index;
    for (int i = 0; i < N; i++) {
        max = -1;
        for (int j = 0; j <= N; j++) {
            if (max < b[j]) {
                max = b[j];
                index = j;
            }
        }
        cout << index << " " << a[index] << endl;
        b[index] = -1;
    }
}
// 64 位输出请用 printf("%lld")