#include <bits/stdc++.h>
using namespace std;
vector<pair<pair<int, int>, pair<int, int> > > li;
vector<pair<pair<int, int>, pair<int, int> > > tmp;
vector<pair<pair<int, int>, pair<int, int> > > tmp2;
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x, y, z;
    scanf("%d%d%d", &x, &y, &z);
    li.push_back(make_pair(make_pair(x, y), make_pair(z, i)));
  }
  sort(li.begin(), li.end());
  for (int i = 0; i < n; i++) {
    if (tmp.size() > 0 && tmp.back().first == li[i].first) {
      printf("%d %d\n", tmp.back().second.second, li[i].second.second);
      tmp.pop_back();
    } else {
      tmp.push_back(li[i]);
    }
  }
  for (int i = 0; i < tmp.size(); i++) {
    if (tmp2.size() > 0 && tmp2.back().first.first == tmp[i].first.first) {
      printf("%d %d\n", tmp2.back().second.second, tmp[i].second.second);
      tmp2.pop_back();
    } else {
      tmp2.push_back(tmp[i]);
    }
  }
  for (int i = 0; i < tmp2.size(); i += 2) {
    printf("%d %d\n", tmp2[i].second.second, tmp2[i + 1].second.second);
  }
  return 0;
}
