#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> next(n + 1);
  vector<int> prev(n + 1);
  vector<int> ans;
  ans.push_back(0);
  int a, b;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    next[i] = b;
    prev[i] = a;
  }
  for (int i = 1; i <= n; i++)
    if (prev[i] == 0) {
      int it = i;
      while (it != 0) {
        ans.push_back(it);
        it = next[it];
      }
    }
  ans.push_back(0);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i == ans[j]) cout << ans[j - 1] << " " << ans[j + 1] << '\n';
  return 0;
}
