#include <bits/stdc++.h> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int, int> pii; typedef pair<ll, ll> pll; typedef vector<long long> vll; typedef vector<int> vi; #define io ios_base::sync_with_stdio(false) #define pb push_back #define isSet(n, i) bool(n & (1LL << i)) #define Set(n, i) (n | (1LL << i)) #define unSet(n, i) (n & !(1LL << i)) #define PI 2 * acos(0.0) #define all(r) (r).begin(), (r).end() #define rev(r) (r).rbegin(), (r).rend() #define dbg(a) cout << #a << " ->->->-> " << a << "\n" #define inf 1000000000000000000 #define mod 1000000007 #define N 1000000 int dirx[] = {1, -1, 0, 0, 1, 1, -1, -1}, diry[] = {0, 0, 1, -1, 1, -1, 1, -1}; map<pll, pll> ans; map<pll, int> pres; ll md(pll a, pll b) { return abs(a.first - b.first) + abs(a.second - b.second); } void rec(int x, int y) { for (int i = 0; i < 4; i++) { int tx = x + dirx[i], ty = y + diry[i]; if (tx <= N && tx >= -N && ty <= N && ty >= -N) { if (pres[{tx, ty}] == 0) { ans[{x, y}] = {tx, ty}; return; } } } for (int i = 0; i < 4; i++) { int tx = x + dirx[i], ty = y + diry[i]; if (tx <= N && tx >= -N && ty <= N && ty >= -N) { if (ans[{tx, ty}].first == INT_MAX) { rec(tx, ty); } if (i == 0) { ans[{x, y}] = ans[{tx, ty}]; } else if (md(ans[{tx, ty}], {x, y}) < md(ans[{x, y}], {x, y})) { ans[{x, y}] = ans[{tx, ty}]; } } } } int main() { io; ifstream in("input.txt"); ofstream out("output.txt"); int n; cin >> n; vector<pll> inp(n); for (int i = 0; i < n; i++) { ll x, y; cin >> x >> y; inp[i] = {x, y}; pres[{x, y}] = 1; ans[{x, y}] = {INT_MAX, INT_MAX}; } for (int i = 0; i < n; i++) { if (ans[inp[i]].first == INT_MAX) { rec(inp[i].first, inp[i].second); } } for (int i = 0; i < n; i++) { cout << ans[inp[i]].first << " " << ans[inp[i]].second << "\n"; } return 0; }