#include <bits/stdc++.h>
using namespace std;
vector<bool> trust;
vector<int> massive(int** mas, int num) {
  vector<int> mase;
  int nume = num;
  int nume1;
  while (nume != 0) {
    nume1 = nume;
    nume = mas[nume - 1][0];
  }
  mase.push_back(0);
  while (nume1 != 0) {
    mase.push_back(nume1);
    trust[nume1 - 1] = true;
    nume1 = mas[nume1 - 1][1];
  }
  mase.push_back(0);
  return mase;
}
int main() {
  int n;
  cin >> n;
  int** mas = new int*[n];
  for (int i = 0; i < n; i++) {
    mas[i] = new int[2];
    trust.push_back(false);
    cin >> mas[i][0] >> mas[i][1];
  }
  vector<vector<int>> comp;
  for (int i = 0; i < n; i++) {
    vector<int> vect2;
    if (!trust[i]) {
      vect2 = massive(mas, i + 1);
      comp.push_back(vect2);
    }
  }
  vector<int> ansver;
  ansver.push_back(0);
  for (auto i : comp) {
    for (auto j : i) {
      if (j != 0) {
        ansver.push_back(j);
      }
    }
  }
  ansver.push_back(0);
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < ansver.size(); j++) {
      if (i == ansver[j]) {
        cout << ansver[j - 1] << " " << ansver[j + 1];
      }
    }
    cout << endl;
  }
}
