#include <bits/stdc++.h>
using namespace std;
int F[105], N[105], book[105], k;
void Find(int x, int s) {
  if (N[x] == 0) {
    book[x] = s;
    return;
  }
  book[x] = s;
  Find(N[x], s);
}
int main() {
  int n, last1, Front, last2;
  k = 1;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", &F[i], &N[i]);
  for (int i = 1; i <= n; i++)
    if (F[i] == 0 && book[i] == 0) Find(i, k++);
  for (int i = 1; i <= n; i++) {
    if (N[i] == 0 && book[i] == 1) last1 = i;
  }
  last2 = last1;
  for (int i = 2; i < k; i++) {
    last1 = last2;
    for (int j = 1; j <= n; j++) {
      if (N[j] == 0 && book[j] == i) last2 = j;
      if (F[j] == 0 && book[j] == i) Front = j;
    }
    F[Front] = last1;
    N[last1] = Front;
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", F[i], N[i]);
  return 0;
}
