#include <bits/stdc++.h>
using namespace std;
bool vis[10010];
pair<int, int> P[200];
int main() {
  int n;
  scanf("%d", &n);
  int begin = -1;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", &P[i].first, &P[i].second);
    if (P[i].first == 0 && begin == -1) begin = i, vis[i] = true;
  }
  int ka = begin;
  int pre = begin;
  while (true) {
    pre = begin;
    while (begin != 0) {
      pre = begin;
      begin = P[begin].second;
    }
    bool K = 0;
    for (int i = 1; i <= n; i++) {
      if (P[i].first == 0 && !vis[i]) {
        begin = i, vis[i] = true;
        K = 1;
        break;
      }
    }
    if (!K) break;
    P[begin].first = pre;
    P[pre].second = begin;
  }
  for (int i = 1; i <= n; i++) printf("%d %d\n", P[i].first, P[i].second);
  return 0;
}
