#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
int N;
int L[MAXN];
int R[MAXN];
vector<int> v;
int main() {
  ios::sync_with_stdio(0);
  cin >> N;
  for (int i = 1; i <= N; i++) {
    cin >> L[i] >> R[i];
    if (L[i] == 0) v.push_back(i);
  }
  for (int i = 0, prv = 0; i < v.size(); i++) {
    int cur = v[i];
    R[prv] = cur;
    L[cur] = prv;
    while (R[cur] != 0) cur = R[cur];
    prv = cur;
  }
  for (int i = 1; i <= N; i++) cout << L[i] << " " << R[i] << "\n";
  return 0;
}
