#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int n, x, y, z;
  vector<pair<pair<long long int, pair<long long int, long long int>>,
              long long int>>
      v;
  cin >> n;
  for (long long int i = 0; i < n; i++) {
    cin >> x >> y >> z;
    v.push_back({{x, {y, z}}, i + 1});
  }
  sort(v.begin(), v.end());
  long long int a[100005] = {0};
  for (long long int i = 0; i <= n; i++) a[i] = -1;
  for (long long int i = 0; i < n; i++) a[i] = v[i].second;
  for (long long int i = 0; i < n; i++) {
    if (i < n - 1 && a[i] != -1 && a[i + 1] != -1 &&
        v[i].first.first == v[i + 1].first.first &&
        v[i].first.second.first == v[i + 1].first.second.first) {
      cout << a[i] << " " << a[i + 1] << endl;
      a[i] = a[i + 1] = -1;
      i++;
    }
  }
  long long int pos = -1;
  for (long long int i = 0; i < n; i++) {
    if (a[i] != -1 && pos == -1)
      pos = i;
    else if (a[i] != -1 && v[i].first.first == v[pos].first.first) {
      cout << a[i] << " " << a[pos] << endl;
      a[i] = a[pos] = -1;
      pos = -1;
    } else if (a[i] != -1)
      pos = i;
  }
  pos = -1;
  for (long long int i = 0; i < n; i++) {
    if (a[i] != -1 && pos == -1)
      pos = i;
    else if (a[i] != -1) {
      cout << a[i] << " " << a[pos] << endl;
      a[i] = a[pos] = -1;
      pos = -1;
    }
  }
}
