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