#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  vector<pair<pair<int, int>, pair<int, int> > > ar(n);
  for (int i = 0; i < n; i++) {
    cin >> ar[i].first.first >> ar[i].first.second >> ar[i].second.first;
    ar[i].second.second = i;
  }
  sort(ar.begin(), ar.end());
  vector<pair<int, int> > ans;
  vector<pair<pair<int, int>, pair<int, int> > > br;
  for (int i = 1; i < n; i++) {
    if (ar[i].first == ar[i - 1].first) {
      ans.push_back({ar[i].second.second, ar[i - 1].second.second});
      i++;
      if (i == (n - 1)) {
        br.push_back(ar[i]);
      }
      continue;
    } else {
      br.push_back(ar[i - 1]);
    }
    if (i + 1 == n) br.push_back(ar[i]);
  }
  ar.clear();
  sort(br.begin(), br.end());
  n = br.size();
  for (int i = 1; i < n; i++) {
    if (br[i].first.first == br[i - 1].first.first) {
      ans.push_back({br[i].second.second, br[i - 1].second.second});
      i++;
      if (i == n - 1) {
        ar.push_back(br[i]);
      }
      continue;
    } else {
      ar.push_back(br[i - 1]);
    }
    if (i + 1 == n) ar.push_back(br[i]);
  }
  sort(ar.begin(), ar.end());
  n = ar.size();
  for (int i = 0; i < n; i += 2) {
    ans.push_back({ar[i].second.second, ar[i + 1].second.second});
  }
  for (int i = 0; i < ans.size(); i++)
    cout << ans[i].first + 1 << " " << ans[i].second + 1 << "\n";
  return 0;
}
