#include <bits/stdc++.h>
using namespace std;
struct Point {
  int x, y, z, id;
} p[50010];
bool cmp(Point a, Point b) {
  return a.x < b.x || (a.x == b.x && a.y < b.y) ||
         (a.x == b.x && a.y == b.y && a.z < b.z);
}
int work(int l, int r, int kind) {
  if (kind == 1) {
    int pos = l, now = 0;
    while (pos <= r) {
      int to = pos;
      while (to < r && p[to + 1].x == p[pos].x) to++;
      int tmp = work(pos, to, 2);
      if (tmp) {
        if (now) {
          printf("%d %d\n", now, tmp);
          now = 0;
        } else
          now = tmp;
      }
      pos = to + 1;
    }
    return now;
  } else if (kind == 2) {
    int pos = l, now = 0;
    while (pos <= r) {
      int to = pos;
      while (to < r && p[to + 1].y == p[pos].y) to++;
      int tmp = work(pos, to, 3);
      if (tmp) {
        if (now) {
          printf("%d %d\n", now, tmp);
          now = 0;
        } else
          now = tmp;
      }
      pos = to + 1;
    }
    return now;
  } else {
    for (int i = l; i < r; i += 2) printf("%d %d\n", p[i].id, p[i + 1].id);
    if ((r - l + 1) & 1) return p[r].id;
    return 0;
  }
}
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d %d %d", &p[i].x, &p[i].y, &p[i].z);
    p[i].id = i;
  }
  sort(p + 1, p + n + 1, cmp);
  work(1, n, 1);
  return 0;
}
