#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;
    auto it = l.begin();
    long long left = *it, right;
    l.erase(left);
    for (auto it1 = r.begin(); it1 != r.end(); it1++) {
      if (*it1 == left) continue;
      bool chosen = true;
      right = *it1;
      for (long long k = left;;) {
        k = a[k].second;
        if (!k) break;
        if (k == right) {
          chosen = false;
          break;
        }
      }
      if (chosen) {
        break;
      }
    }
    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";
  }
}
