#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
int l[maxn], r[maxn];
std::vector<int> nl;
int main(int argc, char const *argv[]) {
  int n;
  cin >> n;
  int sz = 0;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
    if (l[i] == 0) nl.push_back(i), sz++;
  }
  int last = 0, pp = 1;
  while (sz > 1) {
    if (last == 0) {
      int p = nl[0];
      while (r[p]) p = r[p];
      last = p;
    }
    r[last] = nl[pp];
    l[nl[pp]] = last;
    pp++, sz--;
    while (r[last]) last = r[last];
  }
  for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
  return 0;
}
