#include <bits/stdc++.h>
using namespace std;
int par[105], nex[105];
vector<int> prev_less;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, l, r;
  cin >> n;
  int head = 0, x;
  for (int i = 1; i <= n; i++) {
    cin >> l;
    cin >> r;
    if (l == 0 && head == 0) {
      head = i;
    } else if (l == 0) {
      prev_less.push_back(i);
    }
    if (l != 0) {
      par[i] = l;
    }
    if (r != 0) {
      nex[i] = r;
    }
  }
  x = head;
  while (nex[x] != 0) {
    x = nex[x];
  }
  int y;
  for (int i = 0; i < prev_less.size(); i++) {
    y = prev_less[i];
    if (y == head) {
      continue;
    }
    par[y] = x;
    nex[x] = y;
    while (nex[y] != 0) {
      y = nex[y];
    }
    x = y;
  }
  for (int i = 1; i <= n; i++) {
    cout << par[i] << " " << nex[i] << "\n";
  }
  return 0;
}
