#include <bits/stdc++.h>
using namespace std;
int l[105];
int r[105];
int main() {
  int n;
  scanf("%d", &n);
  vector<int> left;
  vector<int> right;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    if (l[i] == 0) {
      left.push_back(i);
    }
    if (r[i] == 0) {
      right.push_back(i);
    }
  }
  for (int i = 0; i < left.size() - 1; i++) {
    int shouldBeChangedLeft = left[i];
    int inVaildRight = shouldBeChangedLeft;
    while (r[inVaildRight] != 0) {
      inVaildRight = r[inVaildRight];
    }
    int chosenRight;
    for (int i = 1; i <= n; i++) {
      if (r[i] == 0 && i != inVaildRight) {
        chosenRight = i;
        break;
      }
    }
    l[shouldBeChangedLeft] = chosenRight;
    r[chosenRight] = shouldBeChangedLeft;
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d\n", l[i], r[i]);
  }
  return 0;
}
