#include <bits/stdc++.h>
using namespace std;
struct po {
  int x, y, z, id;
};
bool cmp(po aa, po bb) {
  if (aa.x == bb.x) {
    if (aa.y == bb.y) return aa.z < bb.z;
    return aa.y < bb.y;
  }
  return aa.x < bb.x;
}
po a[100050];
int vis[100050];
int used[100050];
map<int, int> mp;
vector<int> G[50050];
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &a[i].x, &a[i].y, &a[i].z);
    a[i].id = i;
  }
  sort(a, a + n, cmp);
  vector<pair<int, int> > ans;
  int cnt = 0;
  for (int i = 0; i < n; i++) {
    if (!mp[a[i].x]) mp[a[i].x] = ++cnt;
    G[cnt].push_back(i);
  }
  for (int k = 1; k <= cnt; k++) {
    for (int i = 0; i < (int)G[k].size() - 1; i += 2) {
      int p = G[k][i];
      int q = G[k][i + 1];
      int id1 = a[p].id;
      int id2 = a[q].id;
      if (a[p].y == a[q].y) {
        ans.push_back(pair<int, int>(id1, id2));
        vis[i] = vis[i + 1] = 1;
        used[p] = used[q] = 1;
        continue;
      }
      i--;
    }
    int f = 0;
    int pre = 0;
    for (int i = 0; i < (int)G[k].size(); i++) {
      if (!vis[i]) {
        if (f) {
          ans.push_back(pair<int, int>(a[pre].id, a[G[k][i]].id));
          used[pre] = used[G[k][i]] = 1;
        } else {
          pre = G[k][i];
        }
        f ^= 1;
      }
      vis[i] = 0;
    }
  }
  int f = 0;
  int pre = 0;
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      if (f) {
        ans.push_back(pair<int, int>(a[i].id, a[pre].id));
      } else {
        pre = i;
      }
      f ^= 1;
    }
  }
  for (auto t : ans) {
    printf("%d %d\n", t.first + 1, t.second + 1);
  }
  return 0;
}
