#pragma comment(linker, "/STACK:10240000,10240000")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <cmath>
#include <algorithm>
using namespace std;
#define fst first
#define snd second
pair< int , int > node[123456];
int a[123456], b[6666666];
int fa1[123456], fa2[123456];
int getfa1( int u) { return u == fa1[u]? u : fa1[u] = getfa1(fa1[u]); }
int getfa2( int u) { return u == fa2[u]? u : fa2[u] = getfa2(fa2[u]); }
int main() {
#ifndef ONLINE_JUDGE
freopen ( "in.txt" , "r" , stdin);
#endif // ONLINE_JUDGE
int n;
while (cin >> n, n) {
vector<pair< int , int > > out(n + 1);
for ( int i = 0; i <= n + 1; i ++) fa1[i] = fa2[i] = i;
for ( int i = 1; i <= n; i ++) {
scanf ( "%d%d" , &node[i].fst, &node[i].snd);
a[i] = node[i].snd;
b[a[i]] = node[i].fst;
}
sort(a + 1, a + 1 + n);
for ( int i = n; i > 1; i --) {
int pos = lower_bound(a + 1, a + 1 + n, node[i].snd) - a;
int L = getfa1(pos - 1), R = getfa2(pos + 1);
int dif1 = 1e9, dif2 = 1e9;
if (L > 0) dif1 = a[pos] - a[L];
if (R <= n) dif2 = a[R] - a[pos];
if (dif1 <= dif2) out[i] = make_pair(node[i].fst, b[a[L]]);
else out[i] = make_pair(node[i].fst, b[a[R]]);
fa1[pos] = pos - 1;
fa2[pos] = pos + 1;
}
out[1] = make_pair(node[1].fst, 1);
for ( int i = 1; i <= n; i ++) printf ( "%d %d\n" , out[i].fst, out[i].snd);
}
return 0;
}
|