#include <bits/stdc++.h>
using namespace std;
int l[105], r[105];
int n;
bool used;
inline int find(int now) {
  if (!r[now]) return now;
  return find(r[now]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]);
  for (int i = 1; i <= n; i++)
    if (!l[i]) {
      if (!used) {
        used = 1;
        continue;
      }
      for (int j = 1; j <= n; j++)
        if (j != i && !r[j] && find(r[i]) != j) {
          r[j] = i, l[i] = j;
          break;
        };
    }
  for (int i = 1; i <= n; i++) printf("%d %d\n", l[i], r[i]);
  return 0;
}
