#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;
const int man = 2e5 + 10;
template <typename T>
T gcd(T a, T b) {
  return b == 0 ? a : gcd(b, a % b);
}
template <typename T>
T exgcd(T a, T b, T &g, T &x, T &y) {
  if (!b) {
    g = a, x = 1, y = 0;
  } else {
    exgcd(b, a % b, g, y, x);
    y -= x * (a / b);
  }
}
const long long mod = 1e9 + 7;
struct point {
  int x, y, z, id;
  point() {}
  point(int x, int y, int z, int id) {
    this->x = x;
    this->y = y;
    this->z = z;
    this->id = id;
  }
  bool operator<(point const &a) const {
    if (x == a.x) {
      if (y == a.y) {
        return z < a.z;
      } else {
        return y < a.y;
      }
    } else {
      return x < a.x;
    }
  }
} b[man];
int vis[man];
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    b[i] = point(x, y, z, i);
  }
  sort(b + 1, b + 1 + n);
  int l = 1;
  for (int i = l + 1; i <= n; i++) {
    if (l && b[l].x == b[i].x && b[l].y == b[i].y) {
      vis[i] = vis[l] = 1;
      printf("%d %d\n", b[i].id, b[l].id);
      l = 0;
    } else {
      l = i;
    }
  }
  l = 1;
  while (vis[l]) l++;
  for (int i = l + 1; i <= n; i++) {
    if (vis[i]) continue;
    if (l && b[l].x == b[i].x) {
      vis[i] = vis[l] = 1;
      printf("%d %d\n", b[i].id, b[l].id);
      l = 0;
    } else {
      l = i;
    }
  }
  l = 1;
  while (vis[l]) l++;
  for (int i = l + 1; i <= n; i++) {
    if (vis[i]) continue;
    if (l) {
      printf("%d %d\n", b[i].id, b[l].id);
      l = 0;
    } else {
      l = i;
    }
  }
  return 0;
}
