#include <bits/stdc++.h>
using namespace std;
set<int> lft;
set<int> rt;
int b4[105], af[105];
int main() {
  int n;
  cin >> n;
  vector<int> v;
  for (int i = 1; i <= n; i++) {
    cin >> b4[i] >> af[i];
    if (!b4[i]) v.push_back(i);
  }
  int j = 0;
  for (int i = 0; i < v.size() - 1; i++) {
    for (j = v[i]; af[j]; j = af[j])
      ;
    af[j] = v[i + 1];
    b4[v[i + 1]] = j;
  }
  for (int i = 1; i <= n; i++) cout << b4[i] << ' ' << af[i] << '\n';
  return 0;
}
