#include <bits/stdc++.h>
using namespace std;
long n, nex[100005], trc[100005];
bool av[100005];
struct banghi {
  long x, y, z, id;
} a[100005];
bool cmp(banghi a, banghi b) {
  if (a.x == b.x && a.y == b.y) return a.z < b.z;
  if (a.x == b.x) return a.y < b.y;
  return a.x < b.x;
}
void xoa(long i) {
  av[i] = true;
  nex[trc[i]] = nex[i];
  trc[nex[i]] = trc[i];
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long i = 1; i <= n; ++i) {
    cin >> a[i].x >> a[i].y >> a[i].z;
    a[i].id = i;
  }
  sort(a + 1, a + 1 + n, cmp);
  for (long i = 1; i <= n; ++i) {
    nex[i] = i + 1;
    trc[i] = i - 1;
  }
  for (long i = 1; i <= n; ++i) {
    long u = nex[i];
    if (u == n + 1) continue;
    if (av[u] || av[i]) continue;
    if (a[i].x == a[u].x && a[i].y == a[u].y) {
      cout << a[i].id << " " << a[u].id << endl;
      xoa(i);
      xoa(u);
    }
  }
  for (long i = 1; i <= n; ++i) {
    long u = nex[i];
    if (u == n + 1) continue;
    if (av[u] || av[i]) continue;
    if (a[i].x == a[u].x) {
      cout << a[i].id << " " << a[u].id << endl;
      xoa(i);
      xoa(u);
    }
  }
  for (long i = 1; i <= n; ++i) {
    long u = nex[i];
    if (u == n + 1) continue;
    if (av[u] || av[i]) continue;
    cout << a[i].id << " " << a[u].id << endl;
    xoa(i);
    xoa(u);
  }
  return 0;
}
