#include <bits/stdc++.h>
using namespace std;
int main() {
  std::ios::sync_with_stdio(0);
  cin.tie(0);
  long long n;
  cin >> n;
  vector<pair<long long, long long> > a(n + 5, {0, 0});
  set<long long> l, r;
  for (long long i = 1; i <= n; i++) {
    cin >> a[i].first >> a[i].second;
    if (a[i].first == 0) l.insert(i);
    if (a[i].second == 0) r.insert(i);
  }
  for (long long i = 1; i <= n; i++) {
    if (l.size() == 1 and r.size() == 1) break;
    long long left = *l.begin(), right;
    long long right_end = left;
    while (a[right_end].second) right_end = a[right_end].second;
    for (auto it : r) {
      if (it == left or it == right_end) continue;
      right = it;
      break;
    }
    l.erase(left);
    r.erase(right);
    a[left].first = right;
    a[right].second = left;
  }
  for (long long i = 1; i <= n; i++)
    cout << a[i].first << " " << a[i].second << "\n";
}
