#include <bits/stdc++.h>
using namespace std;
int L[(110)], R[(110)];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) scanf("%d %d\n", &L[i], &R[i]);
  int pre = 0;
  for (int i = 1; i <= n; ++i)
    if (L[i] == 0) {
      L[i] = pre;
      R[pre] = i;
      int w = R[i];
      for (; R[w]; w = R[w])
        ;
      pre = w;
    }
  for (int i = 1; i <= n; ++i) printf("%d %d\n", L[i], R[i]);
  return 0;
}
