#include <bits/stdc++.h>
struct node {
  int l, r;
};
int main() {
  int n;
  scanf("%d", &n);
  node link[n + 10];
  int if_he[n + 10];
  int ct[n + 10];
  for (int i = 1; i <= n; i++) {
    ct[i] = 0;
    scanf("%d%d", &link[i].l, &link[i].r);
    if (!link[i].l)
      if_he[i] = 1;
    else
      if_he[i] = 0;
  }
  int now = 1;
  while (!if_he[now]) now++;
  for (int i = 0; i < n; i++) {
    ct[now]++;
    if (link[now].r)
      now = link[now].r;
    else
      for (int i = 1; i <= n; i++)
        if (!ct[i] && if_he[i]) {
          link[now].r = i;
          link[i].l = now;
          now = i;
          break;
        }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d\n", link[i].l, link[i].r);
  }
}
