#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, l[128], r[128], x, y, flag;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
  x = y = flag = 1;
  while (l[x]) x = l[x];
  while (flag) {
    flag = 0;
    while (r[y]) y = r[y];
    for (int i = 1; i <= n; i++)
      if (!l[i] && i != x) {
        l[i] = y;
        r[y] = i;
        flag = 1;
        break;
      }
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", l[i], r[i]);
}
