#include <bits/stdc++.h>
using namespace std;
class node {
 public:
  int data;
  node *prev, *next;
  node(int i) {
    data = i;
    prev = nullptr;
    next = nullptr;
  }
};
unordered_map<int, node*> mm;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  int n;
  cin >> n;
  int a, b;
  for (int i = 1; i <= n; i++) mm[i] = new node(i);
  for (int i = 1; i <= n; i++) {
    cin >> a >> b;
    if (b) mm[i]->next = mm[b];
    if (a) mm[i]->prev = mm[a];
  }
  unordered_set<int> s;
  for (int i = 1; i <= n; i++) {
    if (mm[i]->prev == nullptr) s.insert(i);
  }
  node* head = mm[*s.begin()];
  s.erase(s.begin());
  auto it = head;
  while (!s.empty()) {
    while (it->next) it = it->next;
    it->next = mm[*s.begin()];
    mm[*s.begin()]->prev = it;
    s.erase(s.begin());
  }
  it = head;
  map<int, pair<int, int>> sol;
  while (it) {
    if (it->prev == nullptr)
      sol[it->data].first = 0;
    else
      sol[it->data].first = it->prev->data;
    if (it->next == nullptr)
      sol[it->data].second = 0;
    else
      sol[it->data].second = it->next->data;
    it = it->next;
  }
  for (auto& it : (sol))
    cout << it.second.first << " " << it.second.second << endl;
  return 0;
}
