#include <bits/stdc++.h>
using namespace std;
int a[((int)1e6 + 10)];
int b[((int)1e6 + 10)], n;
pair<pair<int, int>, pair<int, int> > p[((int)1e6 + 10)];
vector<int> res;
set<int> d;
void fill(vector<pair<pair<int, int>, pair<int, int> > > &v) {
  sort(v.begin(), v.end());
  int j = -1;
  for (int i = 0; i < v.size(); i++) {
    if (d.count(v[i].second.second)) continue;
    if (j == -1) {
      j = i;
      continue;
    }
    if (v[i].first.first == v[j].first.first &&
        v[i].first.second == v[j].first.second) {
      res.push_back(v[j].second.second);
      res.push_back(v[i].second.second);
      d.insert(v[i].second.second);
      d.insert(v[j].second.second);
      j = -1;
    } else {
      j = i;
    }
  }
}
void fill2(vector<pair<pair<int, int>, pair<int, int> > > &v) {
  sort(v.begin(), v.end());
  int j = -1;
  for (int i = 0; i < v.size(); i++) {
    if (d.count(v[i].second.second)) continue;
    if (j == -1) {
      j = i;
      continue;
    }
    if (v[i].first.first == v[j].first.first) {
      res.push_back(v[j].second.second);
      res.push_back(v[i].second.second);
      d.insert(v[i].second.second);
      d.insert(v[j].second.second);
      j = -1;
    } else {
      j = i;
    }
  }
}
int main() {
  scanf("%d", &n);
  vector<pair<pair<int, int>, pair<int, int> > > v, v2, v3;
  for (int i = 0; i < n; i++) {
    scanf("%d%d%d", &p[i].first.first, &p[i].first.second, &p[i].second.first);
    p[i].second.second = i + 1;
    v.push_back(p[i]);
    pair<pair<int, int>, pair<int, int> > p2 = p[i];
    swap(p2.first.second, p2.second.first);
    v2.push_back(p2);
    pair<pair<int, int>, pair<int, int> > p3 = p[i];
    swap(p3.second.first, p3.first.first);
    v3.push_back(p3);
  }
  fill(v);
  fill(v2);
  fill(v3);
  fill2(v);
  fill2(v2);
  fill2(v3);
  sort(v.begin(), v.end());
  for (int i = 0; i < v.size(); i++) {
    if (d.count(v[i].second.second)) continue;
    res.push_back(v[i].second.second);
  }
  for (int i = 0; i < res.size(); i += 2) {
    printf("%d %d\n", res[i], res[i + 1]);
  }
  return 0;
}
