#include <bits/stdc++.h>
int dx[] = {-1, -1, -1, 0, 1, 1, 1, 0};
int dy[] = {-1, 0, 1, 1, 1, 0, -1, -1};
using namespace std;
long long power(long long a, long long p, long long M) {
  long long r = 1;
  a = a % M;
  while (p > 0) {
    if (p % 2) r = (r * a) % M;
    a = (a * a) % M;
    p /= 2;
  }
  return r;
}
long long gcd(long long a, long long b) {
  if (b == 0) return a;
  return gcd(b, a % b);
}
int n;
deque<int> dq[105];
int l[105], r[105];
bool check[105];
int leftt[105], rightt[105];
void solve() {
  cin >> n;
  if (0)
    cout << "n"
         << " : " << n << endl;
  ;
  for (int i = 1; i < n + 1; i++) {
    int a, b;
    cin >> a >> b;
    l[i] = a, r[i] = b;
  }
  int c = 0;
  deque<int> q;
  if (0)
    cout << "1"
         << " : " << 1 << endl;
  ;
  do {
    q.clear();
    for (int i = 1; i < n + 1; i++) {
      if (!check[i]) {
        if (0)
          cout << "i"
               << " : " << i << endl;
        ;
        q.push_front(i);
        check[i] = 1;
        int x = i;
        while (1) {
          if (l[x])
            q.push_front(l[x]), x = l[x];
          else {
            for (int j = 1; j < n + 1; j++) {
              if (r[j] == x) q.push_front(j), x = j;
            }
          }
          if (check[x]) break;
          check[x] = 1;
        }
        x = i;
        while (r[x]) {
          if (r[x])
            q.push_back(r[x]), x = r[x];
          else {
            for (int j = 1; j < n + 1; j++) {
              if (l[j] == x) q.push_back(j), x = j;
            }
          }
          if (check[x]) break;
          check[x] = 1;
        }
        break;
      }
    }
    if (q.size()) dq[c++] = q;
  } while (q.size());
  if (0)
    cout << "c"
         << " : " << c << endl;
  ;
  vector<int> ans;
  for (int i = 0; i < c; i++) {
    if (0)
      cout << "i"
           << " : " << i << endl;
    ;
    for (auto x : dq[i]) ans.push_back(x);
  }
  for (int i = 0; i < ans.size(); i++) {
    if (i == 0)
      leftt[ans[i]] = 0;
    else
      leftt[ans[i]] = ans[i - 1];
    if (i == ans.size() - 1)
      rightt[ans[i]] = 0;
    else
      rightt[ans[i]] = ans[i + 1];
  }
  for (int i = 1; i < n + 1; i++) cout << leftt[i] << ' ' << rightt[i] << endl;
}
int main() {
  solve();
  return 0;
}
