#include <bits/stdc++.h>
using namespace std;
int prv[101];
int prox[101];
int group[101];
void setgroup(int i, int g) {
  while (prox[i] != 0) {
    group[i] = g;
    i = prox[i];
  }
  group[i] = g;
}
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> prv[i] >> prox[i];
  }
  int curr = 1;
  memset(group, 0, sizeof(group));
  for (int i = 1; i <= n; i++) {
    if (prv[i] == 0) {
      setgroup(i, curr);
      curr++;
    }
  }
  while (true) {
    int i;
    for (i = 1; i <= n; i++) {
      if (prv[i] == 0) break;
    }
    int j;
    for (j = 1; j <= n; j++) {
      if (prox[j] == 0 && group[j] != group[i]) {
        setgroup(i, group[j]);
        prox[j] = i;
        prv[i] = j;
        break;
      }
    }
    if (j > n) break;
  }
  for (int i = 1; i <= n; i++) {
    cout << prv[i] << " " << prox[i] << endl;
  }
}
