#include <bits/stdc++.h>
using namespace std;
int main(void) {
  int n;
  cin >> n;
  set<int> a, b;
  vector<pair<int, int> > ans(n);
  for (int i = 0; i < n; ++i) {
    cin >> ans[i].first >> ans[i].second;
    --ans[i].first;
    --ans[i].second;
    if (ans[i].first == -1) a.insert(i);
    if (ans[i].second == -1) b.insert(i);
  }
  while (a.size() > 1) {
    int node = *a.begin();
    a.erase(a.begin());
    int last = node;
    while (ans[last].second != -1) last = ans[last].second;
    auto nxt = b.begin();
    if (*nxt == last) ++nxt;
    ans[*nxt].second = node;
    ans[node].first = *nxt;
    b.erase(nxt);
  }
  for (int i = 0; i < n; ++i)
    cout << ans[i].first + 1 << ' ' << ans[i].second + 1 << '\n';
  return 0;
}
