#include <bits/stdc++.h>
using namespace std;
vector<int> V;
const int MAXN = 105;
int l[MAXN], r[MAXN], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d%d", l + i, r + i);
  for (int i = 1; i <= n; i++) {
    if (l[i]) continue;
    V.push_back(i);
    int cur = i;
    while (r[cur]) cur = r[cur], V.push_back(cur);
  }
  memset(l, 0, sizeof l);
  memset(r, 0, sizeof r);
  for (int i = 0; i < V.size(); i++) {
    if (i) l[V[i]] = V[i - 1];
    if (i != V.size() - 1) r[V[i]] = V[i + 1];
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", l[i], r[i]);
  return 0;
}
