#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 124;
int l[MAX_N], r[MAX_N], used[124];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
    if (l[i] != 0) r[l[i]] = i;
    if (r[i] != 0) l[r[i]] = i;
  }
  int curr;
  for (int i = 1; i <= n; i++)
    if (l[i] == 0) {
      curr = i;
      break;
    }
  for (int i = 1; i <= n; i++) {
    used[curr] = 1;
    if (r[curr] == 0) {
      for (int j = 1; j <= n; j++) {
        if (used[j] == 0 && l[j] == 0) {
          r[curr] = j, l[j] = curr, curr = j;
          break;
        }
      }
    } else
      curr = r[curr];
  }
  for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
  return 0;
}
