#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, num = 0;
  cin >> n;
  int l[n], r[n];
  for (int i = 0; i < n; i++) {
    cin >> l[i] >> r[i];
  }
  vector<vector<int> > li;
  for (int i = 0; i < n; i++) {
    if (l[i] == 0) {
      vector<int> ar;
      li.push_back(ar);
      li[num].push_back(i + 1);
      num++;
    }
  }
  int j, k;
  for (int i = 0; i < num; i++) {
    j = li[i][0] - 1;
    if (i > 0) {
      l[j] = k + 1;
    }
    k = j;
    while (j != -1) {
      k = j;
      j = r[j] - 1;
    }
    if (num - 1 != i) {
      r[k] = li[i + 1][0];
    }
  }
  for (int i = 0; i < n; i++) {
    cout << l[i] << " " << r[i] << endl;
  }
}
