#include <bits/stdc++.h>
using namespace std;
int n;
int counter = 0;
int l[101], r[101], lm[101], rm[101];
void dfs(int s) {
  if (l[s] != 0) lm[s] = lm[l[s]];
  if (r[s] != 0) dfs(r[s]);
}
void dfs2(int s) {
  if (r[s] != 0) rm[s] = rm[r[s]];
  if (l[s] != 0) dfs2(l[s]);
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> l[i] >> r[i];
  }
  for (int i = 1; i <= n; i++) {
    counter++;
    lm[i] = i;
    rm[i] = i;
    dfs(i);
    dfs2(i);
  }
  set<pair<int, int> > s;
  for (int i = 1; i <= n; i++) {
    s.insert(make_pair(lm[i], rm[i]));
  }
  vector<pair<int, int> > v(begin(s), end(s));
  for (int i = 1; i < v.size(); i++) {
    l[v[i].first] = v[i - 1].second;
    r[v[i - 1].second] = v[i].first;
  }
  for (int i = 1; i <= n; i++) {
    cout << l[i] << " " << r[i] << "\n";
  }
}
