#include <bits/stdc++.h>
using namespace std;
vector<int> checked(101, 0);
int cleared = 0;
int findhead(vector<pair<int, int> >& allnodes) {
  int i = 1;
  for (i = 1; i < allnodes.size(); i++) {
    if (allnodes[i].first == 0 && checked[i] == 0) {
      return i;
    }
  }
}
int traverse(int i, vector<pair<int, int> >& allnodes) {
  if (allnodes[i].second == 0) {
    checked[i] = 1;
    cleared++;
    return i;
  }
  checked[i] = 1;
  cleared++;
  return traverse(allnodes[i].second, allnodes);
}
int main() {
  int n;
  cin >> n;
  vector<pair<int, int> > allnodes;
  pair<int, int> temp;
  temp.first = -2;
  temp.second = -2;
  allnodes.push_back(temp);
  int i, j;
  for (i = 0; i < n; i++) {
    cin >> temp.first >> temp.second;
    allnodes.push_back(temp);
  }
  int temphead = findhead(allnodes);
  int tail = traverse(temphead, allnodes);
  while (cleared != n) {
    temphead = findhead(allnodes);
    allnodes[tail].second = temphead;
    allnodes[temphead].first = tail;
    tail = traverse(temphead, allnodes);
  }
  for (i = 1; i <= n; i++) {
    cout << allnodes[i].first << " " << allnodes[i].second << endl;
  }
  return 0;
}
