#include <bits/stdc++.h>
using namespace std;
template <class T>
inline T bigmod(T p, T e, T M) {
  T ret = 1;
  for (; e > 0; e >>= 1) {
    if (e & 1) ret = (ret * p) % M;
    p = (p * p) % M;
  }
  return (T)ret;
}
template <class T>
inline T gcd(T a, T b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
template <class T>
inline T modinverse(T a, T M) {
  return bigmod(a, M - 2, M);
}
template <class T, class X>
inline bool getbit(T a, X i) {
  T t = 1;
  return ((a & (t << i)) > 0);
}
template <class T, class X>
inline T setbit(T a, X i) {
  T t = 1;
  return (a | (t << i));
}
template <class T, class X>
inline T resetbit(T a, X i) {
  T t = 1;
  return (a & (~(t << i)));
}
int n, l[104], r[104];
int prv;
void dfs(int u) {
  if (r[u] != 0)
    dfs(r[u]);
  else {
    prv = u;
    return;
  }
}
int main() {
  scanf("%d", &n);
  for (int i = (1); i <= (n); i++) scanf("%d %d", &l[i], &r[i]);
  prv = 0;
  for (int i = (1); i <= (n); i++) {
    if (l[i] != 0) continue;
    l[i] = prv;
    r[prv] = i;
    dfs(i);
  }
  for (int i = (1); i <= (n); i++) printf("%d %d\n", l[i], r[i]);
  return 0;
}
