#include <bits/stdc++.h>
using namespace std;
const int N = 1000005;
const int NIL = 500005;
const double EPS = 1e-9;
const int INF = 1e9;
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long lcm(long long a, long long b) { return a * b / gcd(a, b); }
int dcmp(double x, double y) { return fabs(x - y) <= EPS ? 0 : x < y ? -1 : 1; }
long long mod = 1000000007;
long long mod2 = 1000000009;
long long power(long long base, int p) {
  if (!p) return 1;
  long long ret = power(base, p / 2);
  ret *= ret;
  ret %= mod;
  if (p & 1) ret *= base, ret %= mod;
  return ret;
}
long long modInverse(int a) { return power(a, mod - 2); }
int l[102], r[102];
int main() {
  int n;
  cin >> n;
  vector<int> second;
  vector<int> sol;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &l[i], &r[i]);
    if (l[i] == 0) {
      second.push_back(i);
    }
  }
  for (int i = 0; i < ((int)((second).size())); i++) {
    while (second[i] != 0) {
      sol.push_back(second[i]);
      second[i] = r[second[i]];
    }
  }
  memset(l, 0, sizeof(l));
  memset(r, 0, sizeof(r));
  for (int i = 0; i < sol.size(); i++) {
    if (i + 1 < sol.size()) r[sol[i]] = sol[i + 1];
    if (i) l[sol[i]] = sol[i - 1];
  }
  for (int i = 1; i <= n; i++) cout << l[i] << ' ' << r[i] << endl;
  return 0;
}
