#include <bits/stdc++.h>
using namespace std;
long long i, j;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, index;
  cin >> n;
  long long a[n + 1], b[n + 1];
  for (i = 1; i <= n; i++) cin >> a[i] >> b[i];
  for (i = 1; i <= n; i++) {
    if (a[i] == 0) {
      vector<long long> v1;
      v1.push_back(i);
      index = b[i];
      while (index != 0) {
        v1.push_back(index);
        index = b[index];
      }
      for (j = i + 1; j <= n; j++) {
        if (b[j] == 0) {
          vector<long long> v2;
          v2.push_back(j);
          index = a[j];
          while (index != 0) {
            v2.push_back(index);
            index = a[index];
          }
          long long f = 1;
          for (long long ii = 0; ii < v1.size(); ii++) {
            for (long long jj = 0; jj < v2.size(); jj++) {
              if (v1[ii] == v2[jj]) {
                f = 0;
              }
            }
          }
          if (f == 1) {
            a[i] = j;
            b[j] = i;
            break;
          }
        }
      }
    }
    if (b[i] == 0) {
      vector<long long> v1;
      v1.push_back(i);
      index = a[i];
      while (index != 0) {
        v1.push_back(index);
        index = a[index];
      }
      for (j = i + 1; j <= n; j++) {
        if (a[j] == 0) {
          vector<long long> v2;
          v2.push_back(j);
          index = b[j];
          while (index != 0) {
            v2.push_back(index);
            index = b[index];
          }
          long long f = 1;
          for (long long ii = 0; ii < v1.size(); ii++) {
            for (long long jj = 0; jj < v2.size(); jj++) {
              if (v1[ii] == v2[jj]) {
                f = 0;
              }
            }
          }
          if (f == 1) {
            b[i] = j;
            a[j] = i;
            break;
          }
        }
      }
    }
  }
  for (i = 1; i <= n; i++) cout << a[i] << " " << b[i] << endl;
}
