#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vl = vector<ll>;
using vll = vector<vl>;
using pll = pair<ll, ll>;
using vb = vector<bool>;
const ll oo = 0x3f3f3f3f3f3f3f3f;
const double eps = 1e-9;
ll n;
vb visited;
void solve(vl &l, vl &r, ll i) {
  visited[i] = true;
  if (r[i] != -1) {
    solve(l, r, r[i]);
  } else {
    for (ll j = (0); j < (n); j++) {
      if (!visited[j] && l[j] == -1) {
        r[i] = j;
        l[j] = i;
        solve(l, r, r[i]);
        break;
      }
    }
  }
}
int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  vl l(n), r(n);
  for (ll i = (0); i < (n); i++) {
    cin >> l[i] >> r[i];
    l[i]--;
    r[i]--;
  }
  visited = vb(n);
  for (ll i = (0); i < (n); i++) {
    if (l[i] == -1) {
      solve(l, r, i);
    }
  }
  for (ll i = (0); i < (n); i++) cout << l[i] + 1 << ' ' << r[i] + 1 << endl;
}
