#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int N = 100;
int l[N + 1];
int r[N + 1];
int main() {
  ios_base::sync_with_stdio(0);
  int n;
  cin >> n;
  vector<int> begins;
  for (int i = 1; i <= n; ++i) {
    cin >> l[i] >> r[i];
    if (l[i] == 0) {
      begins.push_back(i);
    }
  }
  vector<pair<int, int> > q;
  for (auto i : begins) {
    int a = i;
    int b = i;
    while (r[b] != 0) {
      b = r[b];
    }
    q.push_back({a, b});
  }
  while (q.size() > 1) {
    int a = q.back().first;
    int b = q.back().second;
    q.pop_back();
    int c = q.back().first;
    int d = q.back().second;
    q.pop_back();
    r[b] = c;
    l[c] = b;
    q.push_back({a, d});
  }
  for (int i = 1; i <= n; ++i) {
    cout << l[i] << " " << r[i] << endl;
  }
  return 0;
}
