#include <bits/stdc++.h>
using namespace std;
int n, sum;
int a[1000][2], fa[1000];
int getfa(int x) { return x == fa[x] ? x : fa[x] = getfa(fa[x]); }
int main() {
  scanf("%d", &n);
  sum = n;
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &a[i][0], &a[i][1]);
    if (a[i][0]) {
      if (getfa(a[i][0]) != getfa(i)) sum--, fa[fa[i]] = fa[a[i][0]];
    }
    if (a[i][1]) {
      if (getfa(a[i][1]) != getfa(i)) sum--, fa[fa[i]] = fa[a[i][1]];
    }
  }
  while (sum != 1) {
    sum--;
    bool bz = 0;
    for (int i = 1; i <= n && !bz; i++)
      for (int j = 1; j <= n && !bz; j++)
        if (getfa(i) != getfa(j) && !a[i][1] && !a[j][0]) {
          a[i][1] = j, a[j][0] = i;
          fa[fa[i]] = fa[j];
          bz = 1;
        }
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", a[i][0], a[i][1]);
  return 0;
}
