#include <bits/stdc++.h>
using namespace std;
const int maxn = 100005;
vector<int> a;
int ans[105][2];
int fi[105];
vector<pair<int, int> > gg;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    scanf("%d%d", &ans[i][0], &ans[i][1]);
    if (ans[i][0] == 0) a.push_back(i);
  }
  for (int i = 0; i < a.size(); ++i) {
    int t = a[i];
    while (ans[t][1]) t = ans[t][1];
    gg.push_back(make_pair(a[i], t));
  }
  for (int i = 1; i < gg.size(); ++i) {
    int x = gg[i - 1].second;
    int y = gg[i].first;
    ans[x][1] = y;
    ans[y][0] = x;
  }
  for (int i = 1; i <= n; ++i) printf("%d %d\n", ans[i][0], ans[i][1]);
  return 0;
}
