#include <bits/stdc++.h>
using namespace std;
const int M = 110;
int l[M], r[M];
vector<int> ss;
int n;
int ansl[M], ansr[M];
void dfs(int o) {
  if (o == 0) return;
  ss.emplace_back(o);
  dfs(r[o]);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", l + i, r + i);
  }
  for (int i = 1; i <= n; ++i)
    if (l[i] == 0) {
      ss.emplace_back(i);
      dfs(r[i]);
    }
  for (int i = 0; i < ss.size(); ++i) {
    ansl[ss[i]] = i == 0 ? 0 : ss[i - 1];
    ansr[ss[i]] = i == ss.size() - 1 ? 0 : ss[i + 1];
  }
  for (int i = 1; i <= n; ++i) {
    printf("%d %d\n", ansl[i], ansr[i]);
  }
  return 0;
}
