#include <bits/stdc++.h>
using namespace std;
const int maxn = 10000 + 10;
int n, m, p;
int l[maxn], r[maxn];
int a[maxn], b[maxn];
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]);
  for (int i = 1; i <= n; i++) {
    if (l[i] && r[l[i]] == 0) r[l[i]] = i;
    if (r[i] && l[r[i]] == 0) l[r[i]] = i;
  }
  for (int i = 1; i <= n; i++) {
    if (l[i] == 0) {
      p = i;
      a[++m] = p;
      while (r[p] != 0) p = r[p];
      b[m] = p;
    }
  }
  if (m >= 2) {
    for (int i = 1; i <= m; i++) {
      if (i > 1) l[a[i]] = b[i - 1];
      if (i < n) r[b[i]] = a[i + 1];
    }
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", l[i], r[i]);
  return 0;
}
