#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, pair<int, int> > > v, vv, vvv, temp;
int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    v.push_back(make_pair(make_pair(a, b), make_pair(c, i + 1)));
  }
  sort(v.begin(), v.end());
  vv.clear();
  vector<pair<int, int> > ans;
  for (int i = 0; i <= n; i++) {
    if (vv.size() == 0)
      vv.push_back(v[i]);
    else if (i == n || v[i].first.first != v[i - 1].first.first) {
      vvv.clear();
      for (int j = 0; j < vv.size(); j++) {
        if (j == vv.size() - 1) {
          vvv.push_back(vv[j]);
        } else if (vv[j].first.second == vv[j + 1].first.second) {
          ans.push_back(
              make_pair(vv[j].second.second, vv[j + 1].second.second));
          j++;
        } else {
          vvv.push_back(vv[j]);
        }
      }
      for (int j = 0; j < vvv.size(); j += 2) {
        if (j == vvv.size() - 1) {
          temp.push_back(vvv[j]);
        } else {
          ans.push_back(
              make_pair(vvv[j].second.second, vvv[j + 1].second.second));
        }
      }
      vv.clear();
      if (i < n) vv.push_back(v[i]);
    } else
      vv.push_back(v[i]);
  }
  for (int i = 0; i < temp.size(); i += 2) {
    ans.push_back(make_pair(temp[i].second.second, temp[i + 1].second.second));
  }
  for (int i = 0; i < ans.size(); i++) {
    cout << ans[i].first << " " << ans[i].second << endl;
  }
}
