#include <bits/stdc++.h>
using namespace std;
int l[110], r[110], u[110], v[110], c, tmp, vis[110];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
  for (int i = 1; i <= n; i++) {
    if (l[i]) r[l[i]] = i;
    if (r[i]) l[r[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    int x = i;
    if (!l[i]) {
      if (tmp) {
        r[tmp] = i;
        l[i] = tmp;
      }
      while (r[x]) {
        x = r[x];
      }
      tmp = x;
    }
  }
  for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
  return 0;
}
