#include <bits/stdc++.h>
using namespace std;
int l[105], r[105];
int num;
int fa[105];
int Find(int k) {
  if (k == fa[k])
    return k;
  else
    return fa[k] = Find(fa[k]);
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) fa[i] = i;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &l[i], &r[i]);
    if (r[i] == 0) num++;
    if (l[i] != 0) fa[i] = l[i];
  }
  for (int i = 1; i <= n; i++) {
    if (num == 1) break;
    if (r[i] == 0) {
      for (int j = 1; j <= n; j++)
        if (l[j] == 0 && Find(i) != j) {
          r[i] = j;
          l[j] = i;
          fa[j] = i;
          num--;
          break;
        }
    }
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", l[i], r[i]);
}
