#include <bits/stdc++.h>
using namespace std;
class trio {
 public:
  long long first, second, third;
};
class point {
 public:
  long long x, y, z, ind;
  point() {}
};
bool comp(point a, point b) {
  if (a.x == b.x) {
    if (a.y == b.y) {
      return a.z < b.z;
    } else {
      return a.y < b.y;
    }
  } else {
    return a.x < b.x;
  }
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  long long n;
  cin >> n;
  point arr[n];
  for (long long i = 0; i < n; ++i) {
    cin >> arr[i].x >> arr[i].y >> arr[i].z;
    arr[i].ind = i + 1;
  }
  sort(arr, arr + n, comp);
  long long taken[n + 2];
  memset(taken, 0, sizeof(taken));
  long long i = 0, j = 0;
  while (i + 1 < n) {
    if (arr[i].x == arr[i + 1].x && arr[i].y == arr[i + 1].y) {
      taken[i] = 1;
      taken[i + 1] = 1;
      cout << arr[i].ind << " " << arr[i + 1].ind << endl;
      i += 2;
    } else {
      i++;
    }
  }
  i = 0;
  while (i < n) {
    if (taken[i] == 1) {
      i++;
      continue;
    }
    j = i + 1;
    while (j < n && taken[j]) j++;
    if (i < n && j < n && arr[i].x == arr[j].x) {
      taken[i] = 1;
      taken[j] = 1;
      cout << arr[i].ind << " " << arr[j].ind << endl;
      i = j + 1;
    } else {
      i = j;
    }
  }
  i = 0, j = 0;
  while (i < n) {
    if (taken[i] == 1) {
      i++;
      continue;
    }
    j = i + 1;
    while (j < n && taken[j]) j++;
    if (i < n && j < n) {
      taken[i] = 1;
      taken[j] = 1;
      cout << arr[i].ind << " " << arr[j].ind << endl;
      i = j + 1;
    }
  }
  return 0;
}
