#include <bits/stdc++.h>
using namespace std;
struct Node {
  int d, l, r;
  Node(int a, int b, int c) {
    d = a;
    l = b;
    r = c;
  }
};
int rec(vector<Node> &v, int idx) {
  if (v[idx].r == 0) return idx;
  return rec(v, v[idx].r);
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, a, b;
  cin >> n;
  vector<Node> v(n + 1, Node(0, 0, 0));
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    v[i] = Node(i, a, b);
    if (a == 0) q.push(i);
  }
  vector<bool> marker(n + 1, false);
  int prev = -1;
  while (!q.empty()) {
    int curr = q.front();
    q.pop();
    int nxt = rec(v, curr);
    if (prev != -1) {
      v[curr].l = prev;
      v[prev].r = curr;
      prev = nxt;
    } else
      prev = nxt;
  }
  for (int i = 1; i <= n; i++) cout << v[i].l << " " << v[i].r << endl;
}
