#include <bits/stdc++.h>
using namespace std;
const long long mod = 1000000007;
const unsigned gen_seed =
    std::chrono::system_clock::now().time_since_epoch().count();
std::mt19937_64 gen(gen_seed);
std::vector<int> x, y, z;
int dist(int i, int j) {
  return abs(x[i] - x[j]) + abs(y[i] - y[j]) + abs(z[i] - z[j]);
}
int main() {
  int n, r;
  scanf("%d", &n);
  x.resize(n);
  y.resize(n);
  z.resize(n);
  for (int i = 0; i < n; i++) {
    scanf("%d %d %d", &x[i], &y[i], &z[i]);
  }
  std::vector<int> a(n, 0);
  map<std::pair<int, int>, std::vector<std::pair<int, int> > > s;
  for (int i = 0; i < n; i++) {
    s[make_pair(x[i], y[i])].push_back(make_pair(z[i], i));
  }
  for (auto& u : s) {
    sort((u.second).begin(), (u.second).end());
    while (u.second.size() >= 2) {
      int k = u.second.size();
      int i = u.second[k - 1].second;
      int j = u.second[k - 2].second;
      u.second.pop_back();
      u.second.pop_back();
      printf("%d %d\n", i + 1, j + 1);
      a[i] = 1;
      a[j] = 1;
    }
  }
  map<int, std::vector<std::pair<int, int> > > sy;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) continue;
    sy[x[i]].push_back(make_pair(y[i], i));
  }
  for (auto& u : sy) {
    sort((u.second).begin(), (u.second).end());
    while (u.second.size() >= 2) {
      int k = u.second.size();
      int i = u.second[k - 1].second;
      int j = u.second[k - 2].second;
      u.second.pop_back();
      u.second.pop_back();
      printf("%d %d\n", i + 1, j + 1);
      a[i] = 1;
      a[j] = 1;
    }
  }
  std::vector<std::pair<int, int> > rest;
  for (int i = 0; i < n; i++) {
    if (a[i] == 1) continue;
    rest.push_back(make_pair(x[i], i));
  }
  sort((rest).begin(), (rest).end());
  while (rest.size() >= 2) {
    int k = rest.size();
    int i = rest[k - 1].second;
    int j = rest[k - 2].second;
    rest.pop_back();
    rest.pop_back();
    printf("%d %d\n", i + 1, j + 1);
    a[i] = 1;
    a[j] = 1;
  }
}
