#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:36777216")
using namespace std;
int main() {
  int n;
  cin >> n;
  vector<int> prev(n, -1), nxt(n, -1);
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    prev[i] = a - 1;
    nxt[i] = b - 1;
  }
  vector<pair<int, int>> lists;
  for (int i = 0; i < n; i++) {
    if (prev[i] == -1) {
      int cur = i;
      while (nxt[cur] != -1) cur = nxt[cur];
      lists.emplace_back(i, cur);
    }
  }
  for (int i = 0; i < lists.size() - 1; i++) {
    prev[lists[i + 1].first] = lists[i].second;
    nxt[lists[i].second] = lists[i + 1].first;
  }
  for (int i = 0; i < n; i++) cout << prev[i] + 1 << ' ' << nxt[i] + 1 << '\n';
  return 0;
}
