#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<bitset>
#include <unordered_map>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
#define pb(x) push_back(x)
#define cls(x, val) memset(x, val, sizeof(x))
#define fi first
#define se second
#define mp(x, y) make_pair(x, y)
#define inc(i, l, r) for(int i=l; i<=r; i++)
const int inf = 0x3f3f3f3f;
const int maxn = 1e5+10;
int n;
struct Node{
    int id, val;
    bool operator <(const Node &b) const{
        return val<b.val;
    }
    Node(int _id, int _val):id(_id), val(_val){}
    Node (){}
};
set<Node> s;
int main(){
    ios::sync_with_stdio(false);
    scanf("%d", &n);
    Node temp;
    
    scanf("%d%d", &temp.id, &temp.val);
    cout<<temp.id<<" "<<1<<endl;
    
    s.insert(Node(1, 1000000000));
    s.insert(temp);
    set<Node>::iterator it;
    for(int i=2; i<=n; i++){
        
        scanf("%d%d", &temp.id, &temp.val);
        
        int idx1, idx2;
        it = s.upper_bound(temp);
        if(it == s.begin()){
            
            printf("%d %d\n", temp.id, s.begin()->id);
            
            s.insert(temp);
            continue;
        }
        if(it == s.end()){
            it = s.end();it--;
            
            printf("%d %d\n", temp.id, it->id);
            s.insert(temp);
            continue;
        }
        
        int a1 = (prev(s.lower_bound(temp)))->val;
        idx1  = prev(s.lower_bound(temp))->id;
        int a2 = (s.upper_bound(temp))->val;
        idx2 = (s.upper_bound(temp))->id;
        
        if(abs(a1-temp.val)<abs(a2-temp.val)){
            
            printf("%d %d\n", temp.id, idx1);
        }
        else if(abs(a1-temp.val)>abs(a2-temp.val)){
            
            printf("%d %d\n", temp.id, idx2);
        }
        else {
            
            printf("%d %d\n", temp.id, idx1);
        }
        s.insert(temp);
    }
    return 0;
}