#include <bits/stdc++.h>
using namespace std;
long long int power(long long int a, long long int b, long long int m) {
  long long int pp = (b == 0) ? 1 : power(a, b >> 1, m);
  pp = (pp * pp) % m;
  if (b & 1) pp = (pp * a) % m;
  return pp;
}
vector<long long int> v1;
long long int l[105], r[105];
signed main() {
  long long int n;
  scanf("%lld", &(n));
  long long int st = 0;
  for (long long int i = 1; i <= n; i++) {
    scanf("%lld", &(l[i]));
    scanf("%lld", &(r[i]));
    if (l[i] == 0 && st == 0)
      st = i;
    else if (l[i] == 0)
      v1.emplace_back(i);
  }
  if (n == 1) {
    cout << l[1] << " " << r[1];
    return 0;
  }
  long long int cur = st;
  long long int j = 0;
  while (j < v1.size()) {
    if (r[cur] != 0)
      cur = r[cur];
    else {
      r[cur] = v1[j];
      l[v1[j]] = cur;
      cur = v1[j];
      j++;
    }
  }
  for (long long int i = 1; i <= n; i++) printf("%lld %lld\n", l[i], r[i]);
  return 0;
}
