#include <bits/stdc++.h>
using namespace std;
const int maxn = 200;
int a[maxn][3], n;
int f[maxn];
int get(int x) {
  if (f[x] == x) return x;
  f[x] = get(f[x]);
  return f[x];
}
void join(int x, int y) {
  if (!y) return;
  int fx = get(x), fy = get(y);
  if (fx != fy) f[fx] = fy;
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) f[i] = i;
  for (int i = 1; i <= n; i++) {
    cin >> a[i][1] >> a[i][2];
    join(i, a[i][1]);
    join(i, a[i][2]);
  }
  for (int i = 1; i <= n; i++) {
    if (a[i][1] == 0) {
      for (int j = 1; j <= n; j++)
        if (get(i) != get(j)) {
          if (a[j][2] == 0) {
            a[i][1] = j;
            a[j][2] = i;
            join(i, j);
            break;
          }
        }
    }
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", a[i][1], a[i][2]);
}
