#include <bits/stdc++.h>
using namespace std;
using uint = unsigned int;
using ll = long long;
using pii = pair<int, int>;
int l[110], r[110];
vector<int> heads;
int main() {
  int i, n, v, u;
  cin >> n;
  for (i = 1; i <= n; ++i) {
    cin >> l[i] >> r[i];
    if (l[i] == 0) heads.push_back(i);
  }
  for (v = heads.back(), heads.pop_back(); !heads.empty(); heads.pop_back()) {
    while (r[v] != 0) v = r[v];
    u = heads.back();
    r[v] = u;
    l[u] = v;
  }
  for (i = 1; i <= n; ++i) cout << l[i] << ' ' << r[i] << '\n';
  return 0;
}
