#include <bits/stdc++.h>
using namespace std;
vector<vector<int>> m(200005);
vector<int> v;
int k = 0;
bool vi[200005];
void dfs(int i) {
  vi[i] = true;
  for (int j = 0; j < m[i].size(); j++) {
    if (!vi[m[i][j]]) dfs(m[i][j]);
  }
  if (m[i].size() == 1) k = i;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x, y;
    cin >> x >> y;
    if (x > 0)
      m[i].push_back(x);
    else
      v.push_back(i);
    if (y > 0) m[i].push_back(y);
  }
  vi[0] = true;
  for (int i = 0; i < v.size(); i++) {
    if (!vi[v[i]]) {
      m[k].push_back(v[i]);
      m[v[i]].insert(m[v[i]].begin(), k);
      dfs(v[i]);
    }
  }
  cout << "\n";
  for (int i = 1; i <= n; i++) {
    if (m[i].size() == 2) {
      if (m[i][1] == 0)
        cout << m[i][1] << " " << m[i][0];
      else
        cout << m[i][0] << " " << m[i][1];
    } else if (m[i].size() == 1) {
      cout << m[i][0] << " 0";
    }
    cout << "\n";
  }
}
