#include <bits/stdc++.h>
using namespace std;
long long parent[100005], x[100005], y[100005], i, n, j;
long long find(long long a) {
  return a == parent[a] ? a : parent[a] = find(parent[a]);
}
void join(long long a, long long b) { parent[find(b)] = find(a); }
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n;
  for (i = 1; i <= n; i++) parent[i] = i;
  for (i = 1; i <= n; i++) {
    cin >> x[i] >> y[i];
    if (x[i] != 0) join(i, x[i]);
    if (y[i] != 0) join(i, y[i]);
  }
  long long left, right, k;
  for (i = 1; i <= n; i++) {
    for (j = i + 1; j <= n; j++) {
      if (find(i) != find(j)) {
        left = find(i);
        right = find(j);
        for (k = 1; k <= n; k++) {
          if (find(k) == right && y[k] == 0) right = k;
        }
        for (k = 1; k <= n; k++) {
          if (find(k) == left && x[k] == 0) left = k;
        }
        x[left] = right;
        y[right] = left;
        join(i, j);
      }
    }
  }
  for (i = 1; i <= n; i++) cout << x[i] << " " << y[i] << endl;
}
