#include <bits/stdc++.h>
using namespace std;
const int maxn = 107;
int pre[maxn], nex[maxn];
vector<int> rcd[maxn];
void Deal(int st, int k) {
  int pos = st;
  rcd[k].push_back(pos);
  while (nex[pos] != 0) {
    pos = nex[pos];
    rcd[k].push_back(pos);
  }
}
int main() {
  int n, num = 0;
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> pre[i] >> nex[i];
  for (int i = 1; i <= n; i++) {
    if (!pre[i]) Deal(i, ++num);
  }
  for (int i = 1; i < num; i++) {
    int t = rcd[i].size();
    int s1 = rcd[i][t - 1], s2 = rcd[i + 1][0];
    nex[s1] = s2;
    pre[s2] = s1;
  }
  for (int i = 1; i <= n; i++) cout << pre[i] << " " << nex[i] << endl;
  return 0;
}
