#include <bits/stdc++.h>
using namespace std;
const int N = (int)3e5 + 123;
int n;
vector<pair<int, int> > ans;
struct node {
  int x, y, z, num;
} a[N];
bool cmp(node x, node y) {
  if (x.z != y.z) return x.z < y.z;
  if (x.y != y.y) return x.y < y.y;
  return x.x < y.x;
}
int main() {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(NULL);
  cin >> n;
  for (int i = 1; i <= n; ++i) cin >> a[i].x >> a[i].y >> a[i].z, a[i].num = i;
  sort(a + 1, a + n + 1, &cmp);
  vector<int> V;
  for (int i = 1; i <= n; i++) {
    int r = i;
    vector<int> v;
    while (r + 1 <= n && a[r + 1].z == a[i].z) r++;
    for (int j = i; j <= r; ++j) {
      int R = j;
      while (R + 1 <= r && a[R + 1].y == a[j].y) R++;
      for (int k = j + 1; k <= R; k += 2)
        ans.push_back(make_pair(a[k - 1].num, a[k].num));
      if ((R - j + 1) % 2) v.push_back(a[R].num);
      j = R;
    }
    for (int j = 1; j < (int)v.size(); j += 2)
      ans.push_back(make_pair(v[j], v[j - 1]));
    if ((int)v.size() % 2) V.push_back(v.back());
    i = r;
  }
  for (int i = 1; i < (int)V.size(); i += 2)
    ans.push_back(make_pair(V[i], V[i - 1]));
  for (auto i : ans) cout << i.first << " " << i.second << endl;
}
