#include <bits/stdc++.h>
using namespace std;
long long mod = 10000000000;
long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long fastexp(long long x, long long n) {
  long long result = 1;
  while (n > 0) {
    if (n % 2 == 1) result *= x;
    x *= x;
    n = n / 2;
  }
  return result;
}
long long modularExp(long long x, long long n, long long mod) {
  long long result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % mod;
    x = ((x % mod) * (x % mod)) % mod;
    n = n / 2;
  }
  return (result) % mod;
}
bool isPrime(long long n) {
  for (long long i = 2; i * i <= n; i++)
    if (n % i == 0) return false;
  return true;
}
bool mycomp(pair<long long, long long> &a, pair<long long, long long> &b) {
  if (a.first != b.first)
    return a.first < b.first;
  else
    return a.second < b.second;
}
vector<long long> v(105), visited(105, 0);
long long dfs(long long node) {
  long long p = 0;
  long long curr = node;
  while (!visited[curr]) {
    visited[curr] = 1;
    curr = v[curr];
    p++;
  }
  if (curr != node)
    return -1;
  else
    return p;
}
void solve() {
  long long n;
  cin >> n;
  long long i;
  vector<long long> l(n + 1), r(n + 1);
  for (i = 1; i <= n; i++) {
    long long x, y;
    cin >> x >> y;
    l[i] = x;
    r[i] = y;
  }
  vector<long long> visited(n + 1, 0);
  vector<pair<long long, long long> > prs;
  for (i = 1; i <= n; i++) {
    if (!visited[i]) {
      long long le = i, re = i;
      while (l[le] != 0) {
        visited[le] = 1;
        le = l[le];
      }
      while (r[re] != 0) {
        visited[re] = 1;
        re = r[re];
      }
      visited[re] = 1;
      visited[le] = 1;
      prs.push_back({le, re});
    }
  }
  long long lstart = prs[0].first;
  long long rstart = prs[0].second;
  for (i = 1; i < prs.size(); i++) {
    long long x = prs[i].first;
    long long y = prs[i].second;
    r[rstart] = x;
    l[x] = rstart;
    rstart = y;
  }
  for (i = 1; i <= n; i++) cout << l[i] << " " << r[i] << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  long long i;
  t = 1;
  while (t--) {
    solve();
  }
}
