#include <bits/stdc++.h>
using namespace std;
long long power(long long _a, long long _b) {
  long long _r = 1;
  while (_b) {
    if (_b % 2 == 1) _r = (_r * _a);
    _b /= 2;
    _a = (_a * _a);
  }
  return _r;
}
long long add(long long a, long long b, long long m = 1000000007) {
  long long x = a + b;
  while (x >= m) x -= m;
  return x;
}
long long sub(long long a, long long b, long long m = 1000000007) {
  long long x = a - b;
  while (x < 0) x += m;
  return x;
}
long long mul(long long a, long long b, long long m = 1000000007) {
  long long x = a * b;
  x %= m;
  return x;
}
long long gcd(long long a, long long b) {
  while (a && b) a > b ? a %= b : b %= a;
  return a + b;
}
long long lcm(long long a, long long b) {
  return (max(a, b) / gcd(a, b)) * min(a, b);
}
long long returnright(long long x, long long lr[], long long rr[],
                      long long n) {
  long long i, o = x;
  while (1) {
    if (rr[o] == 0) break;
    o = rr[o];
    if (rr[o] == 0) break;
  }
  return o;
}
int main() {
  ios_base::sync_with_stdio(false);
  long long n;
  cin >> n;
  long long lr[n + 1], rr[n + 1], i, j;
  for (i = 1; i <= n; i++) cin >> lr[i] >> rr[i];
  vector<long long> le, ri;
  bool ef[n + 1], right[n + 1];
  memset(ef, false, sizeof(ef));
  for (i = 1; i <= n; i++) {
    if (lr[i] == 0) {
      le.push_back(i);
    }
    if (rr[i] == 0) {
      ri.push_back(i);
    }
  }
  for (i = 0; i < le.size() - 1; i++) {
    long long x = returnright(le[i], lr, rr, n);
    rr[x] = le[i + 1];
    lr[le[i + 1]] = x;
  }
  for (i = 1; i <= n; i++) cout << lr[i] << " " << rr[i] << endl;
  return 0;
}
