#include <bits/stdc++.h>
using namespace std;
int l[105];
int r[105];
int id[105];
void dfs(int cur, int i) {
  id[cur] = i;
  if (r[cur]) dfs(r[cur], i);
}
int main() {
  int n, cnt = 0, c2;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    if (!l[i]) cnt++;
  }
  c2 = cnt;
  for (int i = 1; i <= n; i++) {
    if (!l[i]) dfs(i, c2--);
  }
  for (int i = 1; i < cnt; i++) {
    for (int en = 1; en <= n; en++) {
      if (r[en] || id[en] != i) continue;
      for (int st = 1; st <= n; st++) {
        if (l[st] || id[st] != i + 1) continue;
        l[st] = en, r[en] = st;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d\n", l[i], r[i]);
  }
}
