#include <bits/stdc++.h>
using namespace std;
vector<int> in[105], out[105];
int vis[105];
int main() {
  int n, d;
  cin >> n;
  pair<int, int> ara[n + 1];
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &ara[i].first, &ara[i].second);
    in[ara[i].first].push_back(i);
    out[ara[i].second].push_back(i);
  }
  int idx = in[0].back();
  vis[idx] = 1;
  while (1) {
    int f = 0;
    for (int i = 1; i <= n; i++)
      if (vis[i] == 1) f = 1;
    if (f == 0) break;
    while (ara[idx].second != 0) idx = ara[idx].second, vis[idx] = 1;
    while (in[0].empty() == 0 && vis[in[0].back()] == 1) in[0].pop_back();
    if (in[0].empty()) break;
    ara[idx].second = in[0].back();
    ara[in[0].back()].first = idx;
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", ara[i].first, ara[i].second);
  return 0;
}
