#include <bits/stdc++.h>
using namespace std;
int n, l[100010], r[100010];
int main() {
  if (fopen("a.txt", "r")) {
    freopen("a.txt", "r", stdin);
  } else
    ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> l[i] >> r[i];
  vector<int> v;
  for (int i = 1; i <= n; i++)
    if (l[i] == 0) {
      v.push_back(i);
    }
  int now = v.back(), d = 0;
  while (true) {
    now = v.back();
    v.pop_back();
    d++;
    while (r[now] != 0) {
      now = r[now];
      d++;
    }
    if (d == n) break;
    int nex = v.back();
    l[nex] = now;
    r[now] = nex;
  }
  for (int i = 1; i <= n; i++) cout << l[i] << ' ' << r[i] << '\n';
}
