#include <bits/stdc++.h>
using namespace std;
pair<pair<int, int>, pair<int, int> > a[200100];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < (int)(n); ++i) {
    cin >> a[i].first.first >> a[i].first.second >> a[i].second.first;
    a[i].second.second = i + 1;
  }
  sort(a, a + n);
  vector<bool> used(n, false);
  for (int i = 0; i < (int)(n - 1); ++i) {
    if (a[i].first == a[i + 1].first && !used[i] && !used[i + 1]) {
      cout << a[i].second.second << " " << a[i + 1].second.second << endl;
      used[i] = used[i + 1] = true;
    }
  }
  vector<pair<pair<int, int>, pair<int, int> > > b;
  for (int i = 0; i < (int)(n); ++i) {
    if (!used[i]) {
      b.push_back(a[i]);
    }
  }
  n = b.size();
  used.assign(n, false);
  for (int i = 0; i < (int)(n - 1); ++i) {
    if (b[i].first.first == b[i + 1].first.first && !used[i] && !used[i + 1]) {
      cout << b[i].second.second << " " << b[i + 1].second.second << endl;
      used[i] = used[i + 1] = true;
    }
  }
  vector<pair<pair<int, int>, pair<int, int> > > c;
  for (int i = 0; i < (int)(n); ++i) {
    if (!used[i]) {
      c.push_back(b[i]);
    }
  }
  n = c.size();
  for (int i = 0; i < (int)(n / 2); ++i) {
    cout << c[2 * i].second.second << " " << c[2 * i + 1].second.second << endl;
  }
}
