#include <bits/stdc++.h>
using namespace std;
const int mx = 100100;
const int md = 1e9 + 7;
const double EULER = 2.71828182845904523536;
double pitagoras(double x1, double y1, double x2, double y2) {
  return sqrt(pow(abs(x1 - x2), 2) + pow(abs(y1 - y2), 2));
}
int main() {
  int n, arr[110][3], visit[110] = {}, a, p = 1, depan = 0, belakang = 0;
  scanf("%d", &n);
  for (int i = 0; i < n; i += 1) {
    scanf("%d %d", &arr[i][0], &arr[i][1]);
    if (arr[i][0] == 0) depan++;
  }
  for (int i = 0; i < n; i += 1) {
    if (visit[i] == 0) {
      visit[i] = i + 1;
      a = i;
      while (arr[a][0] != 0 and visit[arr[a][0] - 1] == 0) {
        a = arr[a][0] - 1;
        visit[a] = i + 1;
      }
      a = i;
      while (arr[a][1] != 0 and visit[arr[a][1] - 1] == 0) {
        a = arr[a][1] - 1;
        visit[a] = i + 1;
      }
    }
  }
  a = 1;
  while (depan > 1) {
    visit[a - 1] = 1;
    if (arr[a - 1][1] == 0) {
      for (int i = 0; i < n; i += 1) {
        if (visit[a - 1] != visit[i] and arr[i][0] == 0) {
          arr[i][0] = a;
          arr[a - 1][1] = i + 1;
          depan--;
          break;
        }
      }
    }
    a = arr[a - 1][1];
  }
  for (int i = 0; i < n; i += 1) cout << arr[i][0] << " " << arr[i][1] << endl;
}
