#include <bits/stdc++.h>
using namespace std;
template <typename T>
void printRange(T &x) {
  for (auto y : x) cout << y << ' ';
  cout << endl;
}
const int maxn = 110;
int l[maxn];
int r[maxn];
int n;
map<int, bool> vis;
int counter = 1;
struct S {
  int sar;
  int tah;
};
map<int, S> bucket;
void dfs(int i) {
  vis[i] = true;
  if (l[i] == 0) bucket[counter].sar = i;
  if (r[i] == 0) bucket[counter].tah = i;
  if (!vis[l[i]] && l[i]) dfs(l[i]);
  if (!vis[r[i]] && r[i]) dfs(r[i]);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  set<int> sar, tah;
  for (int(i) = (1); (i) <= (n); (i)++) {
    cin >> l[i] >> r[i];
  }
  for (int(i) = (1); (i) <= (n); (i)++) {
    if (!vis[i]) {
      dfs(i);
      counter++;
    }
  }
  for (int(i) = (1); (i) <= (counter - 2); (i)++) {
    r[bucket[i].tah] = bucket[i + 1].sar;
    l[bucket[i + 1].sar] = bucket[i].tah;
  }
  for (int(i) = (1); (i) <= (n); (i)++) {
    cout << l[i] << ' ' << r[i] << endl;
  }
  return 0;
}
