#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n;
  cin >> n;
  int l[n], r[n];
  int op = 0;
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
  }
  int left = -1, right = -1;
  for (int i = 0; i < n; i++) {
    if (l[i] == 0) {
      if (left == -1 && right == -1) {
        left = i + 1;
        right = i + 1;
        while (r[right - 1] != 0) {
          right = r[right - 1];
        }
      } else {
        l[i] = right;
        r[right - 1] = i + 1;
        right = i + 1;
        while (r[right - 1] != 0) {
          right = r[right - 1];
        }
      }
    }
  }
  for (int i = 0; i < n; i++) {
    cout << l[i] << " " << r[i] << "\n";
  }
  return 0;
}
