#include <bits/stdc++.h>
using namespace std;
const int INF = 987654321;
const long long MOD = 1e9 + 7;
const int N = 2e5 + 100;
int l[N], r[N];
vector<pair<int, int> > vec;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
  }
  for (int i = 1; i <= n; i++) {
    if (l[i] == 0) {
      int temp = i;
      while (r[temp] != 0) temp = r[temp];
      vec.push_back(pair<int, int>(i, temp));
    }
  }
  for (int i = 0; i < vec.size() - 1; i++) {
    int a = vec[i].second;
    int b = vec[i + 1].first;
    l[b] = a;
    r[a] = b;
  }
  for (int i = 1; i <= n; i++) cout << l[i] << " " << r[i] << endl;
  return 0;
}
