#include <bits/stdc++.h>
using namespace std;
void remove(vector<pair<pair<int, int>, pair<int, int> > > &data,
            vector<int> &idx, int a, int b) {
  cout << idx[a] << " " << idx[b] << "\n";
  for (int i = b + 1; i < data.size(); ++i) {
    swap(data[i - 1], data[i]);
    swap(idx[i - 1], idx[i]);
  }
  data.pop_back();
  idx.pop_back();
  for (int i = a + 1; i < data.size(); ++i) {
    swap(data[i - 1], data[i]);
    swap(idx[i - 1], idx[i]);
  }
  data.pop_back();
  idx.pop_back();
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t, n, m;
  cin >> n;
  int v = 1;
  vector<pair<pair<int, int>, pair<int, int> > > data(n);
  for (auto &p : data) {
    cin >> p.first.first >> p.first.second >> p.second.first;
    p.second.second = v++;
  }
  sort(data.begin(), data.end());
  vector<int> idx;
  vector<pair<pair<int, int>, pair<int, int> > > temp;
  int pos = 0, a, b, c;
  for (int i = 0; i < n; ++i) {
    while (temp.size() != 4 && i < n) {
      temp.push_back(data[i]);
      idx.push_back(data[i++].second.second);
    }
    i--;
    a = 0, b = 1, c = 2;
    if (temp[a].first.first != temp[b].first.first) {
      if (temp.size() == 2 || (temp[b].first.first != temp[c].first.first)) {
        remove(temp, idx, a, b);
        continue;
      }
      a = b, b = c, ++c;
    }
    if (temp[c].first.first != temp[b].first.first)
      remove(temp, idx, a, b);
    else {
      if (temp[c].first.second != temp[b].first.second)
        remove(temp, idx, a, b);
      else
        remove(temp, idx, b, c);
    }
  }
  if (!temp.empty()) remove(temp, idx, 0, 1);
  return 0;
}
