#include <bits/stdc++.h>
using namespace std;
const int N = 200;
int uf[N];
int root(int x) {
  int r = x;
  while (uf[r] >= 0) r = uf[r];
  int t;
  while (uf[x] >= 0) {
    t = uf[x];
    uf[x] = r;
    x = t;
  }
  return r;
}
void join(int x, int y) {
  x = root(x);
  y = root(y);
  if (x == y) return;
  if (uf[x] < uf[y]) {
    uf[x] += uf[y];
    uf[y] = x;
  } else {
    uf[y] += uf[x];
    uf[x] = y;
  }
}
int main(void) {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(uf, -1, sizeof(uf));
  int n;
  cin >> n;
  vector<int> l(n + 1), r(n + 1), free_left, free_right;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
    if (l[i]) join(i, l[i]);
    if (r[i]) join(i, r[i]);
    if (l[i] == 0) free_right.push_back(i);
    if (r[i] == 0) free_left.push_back(i);
  }
  while ((int)free_left.size() > 1) {
    int y = free_right.back();
    free_right.pop_back();
    int x = free_left.back();
    free_left.pop_back();
    if (root(x) == root(y)) {
      int t = x;
      x = free_left.back();
      free_left.pop_back();
      free_left.push_back(t);
      assert(root(x) != root(y));
    }
    r[x] = y;
    l[y] = x;
    join(x, y);
  }
  for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
  return 0;
}
