#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> r, start, res;
  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;
    if (!a) {
      start.push_back(i + 1);
    }
    cin >> a;
    r.push_back(a);
  }
  for (int i = 0; i < start.size(); i++) {
    res.push_back(start[i]);
    int pos = start[i] - 1;
    while (r[pos]) {
      res.push_back(r[pos]);
      pos = r[pos] - 1;
    }
  }
  for (int i = 1; i <= res.size(); i++) {
    for (int j = 0; j < res.size(); j++) {
      if (i == res[j]) {
        if (!j)
          cout << 0 << ' ';
        else
          cout << res[j - 1] << ' ';
        if (j == res.size() - 1)
          cout << 0 << endl;
        else
          cout << res[j + 1] << endl;
        break;
      }
    }
  }
  return 0;
}
