#include <bits/stdc++.h>
using namespace std;
struct el {
  int l, r, i;
};
int main() {
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  int l[102], r[102];
  vector<vector<int>> lists;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
  }
  for (int i = 1; i <= n; i++) {
    if (l[i] == 0) {
      int cur = i;
      vector<int> curList;
      while (cur != 0) {
        curList.push_back(cur);
        cur = r[cur];
      }
      lists.push_back(curList);
    }
  }
  for (int i = 0; i < lists.size() - 1; i++) {
    vector<int> a = lists[i], b = lists[i + 1];
    int ra = a[a.size() - 1], lb = b[0];
    r[ra] = lb;
    l[lb] = ra;
  }
  cout << endl;
  for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
  return 0;
}
