#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int l[N];
int r[N];
int visited[N];
int main() {
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    scanf("%d %d", l + i, r + i);
  }
  int prevEndNode = 0;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      visited[i] = 1;
      int startNode = i;
      while (l[startNode]) {
        startNode = l[startNode];
        visited[startNode] = 1;
      }
      int endNode = i;
      while (r[endNode]) {
        endNode = r[endNode];
        visited[endNode] = 1;
      }
      if (prevEndNode) {
        l[startNode] = prevEndNode;
        r[prevEndNode] = startNode;
      }
      prevEndNode = endNode;
    }
  }
  for (int i = 1; i <= n; i++) {
    printf("%d %d\n", l[i], r[i]);
  }
}
