#include <bits/stdc++.h>
using namespace std;
int n;
pair<int, int> a[111];
int dfs(int v) {
  if (a[v].second == 0) return v;
  return dfs(a[v].second);
}
int main() {
  srand('D' + 'E' + 'N' + 'I' + 'S' + 'S' + 'O' + 'N' + time(NULL));
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i].first >> a[i].second;
  vector<pair<int, int> > t;
  for (int i = 1; i <= n; i++)
    if (a[i].first == 0) {
      auto now = dfs(i);
      t.push_back(make_pair(i, now));
    }
  for (int i = 1; i < t.size(); i++) {
    a[t[i - 1].second].second = t[i].first;
    a[t[i].first].first = t[i - 1].second;
  }
  for (int i = 1; i <= n; i++) cout << a[i].first << ' ' << a[i].second << "\n";
}
