#include <bits/stdc++.h>
using namespace std;
void rs(int &x) { scanf("%d", &x); }
void rsl(long long &x) { scanf("%I64d", &x); }
void rsd(double &x) { scanf("%lf", &x); }
void rss(char *str) { scanf("%s", str); }
void ws(int &x) { printf("%d ", x); }
void wsl(long long &x) { printf("%I64d ", x); }
void wsd(double &x) { printf("%lf ", x); }
void wss(char *str) { printf("%s ", str); }
long long gcd(long long a, long long b) {
  if (a == 0) return b;
  return gcd(b % a, a);
}
int const N = (1e5) + 5;
int const mod = (1e9) + 7;
int l[105], r[105];
vector<int> beg;
int main() {
  ios::sync_with_stdio(false);
  ifstream in("input.txt");
  ofstream out("output.txt");
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> l[i] >> r[i];
    if (l[i] == 0) {
      beg.push_back(i);
    }
  }
  int st = beg.back();
  beg.pop_back();
  while (r[st] > 0) {
    st = r[st];
  }
  while (beg.size() > 0) {
    int cb = beg.back();
    beg.pop_back();
    r[st] = cb;
    l[cb] = st;
    while (r[cb] > 0) {
      cb = r[cb];
    }
    st = cb;
  }
  for (int i = 1; i <= n; ++i) {
    cout << l[i] << " " << r[i] << endl;
  }
  return 0;
}
