#include <bits/stdc++.h>
using namespace std;
vector<int> g[105];
map<int, int> lefts;
map<int, int> rights;
bool vis[105];
bool found = false;
int bfs(int s) {
  bool isvisited[105];
  memset(isvisited, false, sizeof isvisited);
  isvisited[s] = true;
  queue<int> q;
  q.push(s);
  int res;
  while (!q.empty()) {
    int u = q.front();
    if (rights[u] == 0) {
      res = u;
      break;
    }
    q.pop();
    for (int i = 0; i < g[u].size(); i++) {
      int v = g[u][i];
      if (!isvisited[v]) {
        isvisited[v] = true;
        q.push(v);
      }
    }
  }
  return res;
}
int main() {
  int n;
  cin >> n;
  vector<int> roots;
  for (int i = 1; i <= n; i++) {
    int l, r;
    cin >> l >> r;
    lefts[i] = l;
    rights[i] = r;
    if (r != 0) {
      g[i].push_back(r);
    }
    if (l == 0) {
      roots.push_back(i);
    }
  }
  vector<pair<int, int>> newarr;
  for (int i = 0; i < roots.size(); i++) {
    int u = roots[i];
    found = false;
    int ends = bfs(u);
    newarr.push_back(make_pair(u, ends));
  }
  for (int i = 0; i < newarr.size() - 1; i++) {
    int start = newarr[i].first;
    int fin = newarr[i].second;
    int ns = newarr[i + 1].first;
    int nf = newarr[i + 1].second;
    rights[fin] = ns;
    lefts[ns] = fin;
  }
  for (int i = 1; i <= n; i++) {
    cout << lefts[i] << " " << rights[i] << "\n";
  }
  return 0;
}
