#include <bits/stdc++.h>
using namespace std;
int n, a[105], b[105], cnt, v[105], nod, p;
int32_t main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if (!a[i]) cnt++;
  }
  for (int i = 1; i <= n; i++) {
    if (!a[i]) nod = i;
  }
  p = nod;
  do {
    v[nod] = 1;
    if (!b[nod]) {
      for (int j = 1; j <= n; j++) {
        if (!v[j] && !a[j]) {
          b[nod] = j;
          a[j] = nod;
          break;
        }
      }
    }
    nod = b[nod];
  } while (nod);
  for (int i = 1; i <= n; i++) {
    cout << a[i] << ' ' << b[i] << '\n';
  }
}
