#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<pair<int, int>> vect(n + 1);
  set<int, less<int>> left_open;
  for (int i = 1; i <= n; i++) {
    cin >> vect[i].first >> vect[i].second;
    if (vect[i].first == 0) left_open.insert(i);
  }
  vector<int> final_list;
  final_list.push_back(0);
  int x;
  for (auto it = left_open.begin(); it != left_open.end(); it++) {
    x = *it;
    final_list.push_back(x);
    while (vect[x].second != 0) {
      final_list.push_back(vect[x].second);
      x = vect[x].second;
    }
  }
  final_list.push_back(0);
  for (int i = 1; i <= n; i++) {
    vect[final_list[i]].first = final_list[i - 1];
    vect[final_list[i]].second = final_list[i + 1];
  }
  for (int i = 1; i <= n; i++) {
    cout << vect[i].first << " " << vect[i].second << endl;
  }
  return 0;
}
