#include <bits/stdc++.h>
using namespace std;
struct node {
  long long x, y, z, id;
} a[50005];
long long v[50005];
bool compare(node aa, node bb) {
  if (aa.x != bb.x) {
    return aa.x < bb.x;
  }
  if (aa.y != bb.y) {
    return aa.y < bb.y;
  }
  if (aa.z != bb.z) {
    return aa.z < bb.z;
  }
  return true;
}
int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, id = 0;
  cin >> n;
  for (long 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, compare);
  a[0].x = -1e9;
  for (long long i = 1; i <= n; i++) {
    if (a[i].x == a[i - 1].x && a[i].y == a[i - 1].y) {
      cout << a[i - 1].id << " " << a[i].id << "\n";
      v[i] = v[i - 1] = 1;
      i++;
    }
  }
  for (long long i = 1; i <= n; i++) {
    if (v[i]) continue;
    if (!id) {
      id = i;
      continue;
    }
    if (a[i].x == a[id].x) {
      cout << a[id].id << " " << a[i].id << "\n";
      v[id] = v[i] = 1;
      id = 0;
    } else {
      id = i;
    }
  }
  id = 0;
  for (long long i = 1; i <= n; i++) {
    if (v[i]) continue;
    if (!id) {
      id = i;
      continue;
    }
    cout << a[id].id << " " << a[i].id << "\n";
    v[id] = v[i] = 1;
    id = 0;
  }
}
