#include <bits/stdc++.h>
using namespace std;
const int MaxN = 100 + 5;
const int INF = 0x7F7F7F7F;
int N;
int L[MaxN], R[MaxN];
int Pre[MaxN], Nxt[MaxN];
bool used[MaxN];
void init() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) scanf("%d %d", &L[i], &R[i]);
}
inline int Find_head() {
  for (int i = 1; i <= N; ++i)
    if (used[i] == false && L[i] == 0) return i;
  return -1;
}
void solve() {
  int pre = 0;
  while (Find_head() != -1) {
    int x = Find_head();
    used[x] = true;
    Nxt[pre] = x, Pre[x] = pre, Nxt[x] = R[x];
    pre = x;
    for (int i = R[x]; i != 0; i = R[i]) {
      Nxt[i] = R[i], Pre[i] = L[i];
      used[i] = true;
      pre = i;
    }
  }
  for (int i = 1; i <= N; ++i) printf("%d %d\n", Pre[i], Nxt[i]);
}
int main() {
  init();
  solve();
  return 0;
}
