#include <bits/stdc++.h>
using namespace std;
int left_[105], right_[105], mark[105];
int main() {
  int n, j;
  std::ios::sync_with_stdio(0);
  while (~scanf("%d", &n)) {
    int ans = 0;
    memset(left_, 0, sizeof(left_));
    memset(right_, 0, sizeof(right_));
    memset(mark, 0, sizeof(mark));
    for (int i = 1; i <= n; i++) {
      scanf("%d %d", &left_[i], &right_[i]);
      if (left_[i] == 0) mark[++ans] = i;
    }
    for (int i = 1; i < ans; i++) {
      for (j = mark[i]; right_[j] != 0; j = right_[j])
        ;
      left_[mark[i + 1]] = j;
      right_[j] = mark[i + 1];
    }
    for (int i = 1; i <= n; i++) printf("%d %d\n", left_[i], right_[i]);
  }
}
