#include <bits/stdc++.h>
using namespace std;
const int N = 102;
pair<int, int> ar[N];
bool used[N];
vector<deque<int>> vc;
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> ar[i].first >> ar[i].second;
  for (int i = 1; i <= n; ++i) {
    if (used[i]) continue;
    deque<int> q;
    q.push_back(i);
    int cur = i;
    while (ar[cur].first) {
      used[cur] = true;
      cur = ar[cur].first;
      q.push_front(cur);
    }
    used[cur] = true;
    cur = i;
    while (ar[cur].second) {
      used[cur] = true;
      cur = ar[cur].second;
      q.push_back(cur);
    }
    used[cur] = true;
    vc.push_back(q);
  }
  for (int i = 1; i < vc.size(); ++i) {
    while (!vc[i].empty()) {
      vc[0].push_back(vc[i].front());
      vc[i].pop_front();
    }
  }
  auto cur = vc[0];
  ar[cur.front()].first = ar[cur.back()].second = 0;
  for (int i = 0; i < (int)cur.size() - 1; ++i) ar[cur[i]].second = cur[i + 1];
  for (int i = (int)cur.size() - 1; i > 0; --i) ar[cur[i]].first = cur[i - 1];
  for (int i = 1; i <= n; ++i)
    cout << ar[i].first << " " << ar[i].second << endl;
  return 0;
}
