#include <bits/stdc++.h>
using namespace std;
int a[105], b[105], c[105];
vector<int> v1, v2;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  int k = 0;
  for (int i = 1; i <= n; i++) {
    cin >> a[i] >> b[i];
    if (a[i] == 0) c[k++] = i;
  }
  for (int i = 0; i < k; i++) {
    int j = c[i];
    while (b[j] != 0) j = b[j];
    b[j] = c[i + 1];
    a[c[i + 1]] = j;
  }
  for (int i = 1; i <= n; i++) cout << a[i] << " " << b[i] << endl;
  return 0;
}
