#include <bits/stdc++.h>
using namespace std;
int N;
vector<vector<int> > v;
bool used[50010];
void func_z(int L, int R) {
  int i = L;
  int j;
  int tmp = -1, i2 = -1;
  for (i = L; i < R; i++)
    if (!used[i]) {
      if (tmp == -1) {
        tmp = v[i][3];
        i2 = i;
      } else {
        printf("%d %d\n", tmp, v[i][3]);
        used[i2] = used[i] = true;
        tmp = -1;
      }
    }
}
void func_y(int L, int R) {
  int i = L;
  int j;
  while (i < R) {
    for (j = i; j < R; j++)
      if (v[j][1] != v[i][1]) break;
    func_z(i, j);
    i = j;
  }
  int tmp = -1, i2 = -1;
  for (i = L; i < R; i++)
    if (!used[i]) {
      if (tmp == -1) {
        tmp = v[i][3];
        i2 = i;
      } else {
        printf("%d %d\n", tmp, v[i][3]);
        used[i2] = used[i] = true;
        tmp = -1;
      }
    }
}
void func_x(int L, int R) {
  int i = L;
  int j;
  while (i < R) {
    for (j = i; j < R; j++)
      if (v[j][0] != v[i][0]) break;
    func_y(i, j);
    i = j;
  }
  int tmp = -1, i2 = -1;
  for (i = L; i < R; i++)
    if (!used[i]) {
      if (tmp == -1) {
        tmp = v[i][3];
        i2 = i;
      } else {
        printf("%d %d\n", tmp, v[i][3]);
        used[i2] = used[i] = true;
        tmp = -1;
      }
    }
}
int main(void) {
  int i;
  cin >> N;
  for ((i) = 0; (i) < (int)(N); (i)++) {
    vector<int> w(4);
    scanf("%d%d%d", &w[0], &w[1], &w[2]);
    w[3] = i + 1;
    v.push_back(w);
  }
  sort(v.begin(), v.end());
  func_x(0, N);
  return 0;
}
