#include <bits/stdc++.h>
using namespace std;
int l[110], r[110];
queue<int> Q;
int n;
int find_left(int x) {
  while (l[x] != 0) {
    x = l[x];
  }
  return x;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l[i];
    cin >> r[i];
  }
  for (int i = 1; i <= n; i++) {
    if (r[i] == 0) Q.push(i);
  }
  int tem, now = Q.front(), cur;
  Q.pop();
  while (!Q.empty()) {
    cur = Q.front();
    Q.pop();
    tem = find_left(cur);
    l[tem] = now;
    r[now] = tem;
    now = cur;
  }
  for (int i = 1; i <= n; i++) cout << l[i] << ' ' << r[i] << endl;
  return 0;
}
