#include<algorithm>
#include<set>
#include<map>
#include<iostream>
using namespace std;
multimap<int,int> club;//存储数据
multimap<int,int>::iterator st,st1,st2;
int n,id,power;
int a[100005];
int b[100005];
int main()
{
scanf("%d",&n);
club.insert(make_pair(1000000000,1));
for(int i=0;i<n;i++)
{
scanf("%d%d",&id,&power);
if((club.find(power))!=club.end()) { //如果发现有相同的能力,再比较id,如果插入的小,删除并插入小的
st=club.find(power);
if(id<st->second){
a[i]=id;
b[i]=st->second;
club.erase(st->first);
club.insert(make_pair(power,id));
}
else
{
a[i]=id;
b[i]=st->second;
}
}
else{//如果没有相同能力的,插入后,再进行比较。先看是插入在首或是尾部。
st=club.insert(make_pair(power,id));
if(st==club.begin()){//插在首部,是能力差,把第二个的id放在数组中。
//实力差
a[i]=id;
b[i]=(++st)->second;
}
else if(st==(--club.end())){//插在尾部是能力强,输出倒数第二个的id。
//实力强
st--;
a[i]=id;
b[i]=st->second;
}
else{
st1=++st;
st--;
st2=--st;
if(power-st2->first<st1->first-power){//前后能力不同的比较。
a[i]=id;
b[i]=st2->second;
}
else if(power-st2->first>st1->first-power){
a[i]=id;
b[i]=st1->second;}
else {//如果前后差的能力相同,找到最小的id。
if(st1->second<st2->second){
a[i]=id;
b[i]=st1->second; }
else{
a[i]=id;
b[i]=st2->second; }
}
}//失败了N次啊,也可插入 club.insert(make_pair(power,id));
}
}
for(int i=0;i<n;i++)
printf("%d %d\n",a[i],b[i]);
return 0;
}