#include <bits/stdc++.h>
int zuo[105];
int you[105];
int z[105];
int y[105];
int main() {
  int a;
  while (~scanf("%d", &a)) {
    int zr = 1, yr = 1;
    for (int i = 1; i <= a; i++) {
      scanf("%d%d", &zuo[i], &you[i]);
      if (you[i] == 0) {
        z[zr] = i;
        zr++;
      }
    }
    memset(y, 0, sizeof(y));
    int zc = 1;
    int yc = 0;
    for (int i = 1; i <= a; i++) {
      if (zuo[i] == 0 && zc < zr - 1) {
        int k = you[i];
        while (you[k] != 0) {
          k = you[k];
        }
        if (z[zc] == k || z[zc] == i) {
          int zhong = z[zc];
          zc++;
          while (y[z[zc]] == 1) zc++;
          zuo[i] = z[zc];
          you[z[zc]] = i;
          z[zc] = zhong;
        } else {
          zuo[i] = z[zc];
          you[z[zc]] = i;
          zc++;
        }
        while (y[z[zc]] == 1) zc++;
      }
    }
    for (int i = 1; i <= a; i++) {
      printf("%d %d\n", zuo[i], you[i]);
    }
  }
  return 0;
}
