#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int a[n + 1];
  vector<int> v;
  vector<pair<int, int> > inp(n + 1);
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    inp[i].first = x;
    inp[i].second = y;
    a[x] = i;
    a[i] = y;
    if (x == 0) v.push_back(i);
  }
  int curr, prev = 0;
  for (int i = 0; i < v.size(); i++) {
    curr = v[i];
    while (true) {
      if (a[curr] == 0) break;
      prev = curr;
      curr = a[curr];
    }
    if (i < (v.size() - 1)) {
      inp[curr].second = v[i + 1];
      inp[v[i + 1]].first = curr;
      prev = curr;
    }
  }
  for (int i = 1; i < inp.size(); i++)
    cout << inp[i].first << " " << inp[i].second << endl;
  return 0;
}
