#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long INFF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-9;
const long long mod = 1e9 + 7;
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while (ch<'0' | ch> '9') {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
void Out(int aa) {
  if (aa > 9) Out(aa / 10);
  putchar(aa % 10 + '0');
}
vector<int> pl, pr, p0;
int l[110], r[110];
int main() {
  int n = read();
  for (int i = 1; i <= n; i++) {
    l[i] = read(), r[i] = read();
  }
  int keep = 0;
  for (int i = 1; i <= n; i++) {
    if (!l[i]) {
      int j = i;
      for (; r[j]; j = r[j])
        ;
      if (keep == 0)
        keep = j;
      else {
        l[i] = keep;
        r[keep] = i;
        keep = j;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d\n", l[i], r[i]);
  }
  return 0;
}
