#include <bits/stdc++.h>
using namespace std;
int l[105], r[105];
bool visted[105];
int find_left(int x) {
  visted[x] = true;
  if (l[x] == 0)
    return x;
  else
    return find_left(l[x]);
}
int find_right(int x) {
  visted[x] = true;
  if (r[x] == 0)
    return x;
  else
    return find_right(r[x]);
}
int main() {
  int n;
  scanf("%d", &n);
  memset(visted, false, sizeof(visted));
  for (int i = 1; i <= n; i++) scanf("%d%d", &l[i], &r[i]);
  int left_ = find_left(1);
  int right_ = find_right(1);
  int left_1, right_1;
  for (int i = 2; i <= n; i++) {
    if (!visted[i]) {
      left_1 = find_left(i);
      right_1 = find_right(i);
      l[left_1] = right_;
      r[right_] = left_1;
      right_ = right_1;
    }
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", l[i], r[i]);
  return 0;
}
