#include <bits/stdc++.h>
using namespace std;
int N;
const int MAXN = 5e4 + 15;
int X[MAXN], Y[MAXN], Z[MAXN];
int XX[MAXN], YY[MAXN], ZZ[MAXN];
vector<pair<pair<int, int>, int> > P[MAXN];
int V[MAXN];
int main() {
  ios::sync_with_stdio(false);
  cin >> N;
  for (int i = 0; i < N; ++i) {
    cin >> X[i] >> Y[i] >> Z[i];
    XX[i] = X[i];
    YY[i] = Y[i];
    ZZ[i] = Z[i];
  }
  sort(XX, XX + N);
  sort(YY, YY + N);
  sort(ZZ, ZZ + N);
  int N1 = unique(XX, XX + N) - XX;
  int N2 = unique(YY, YY + N) - YY;
  int N3 = unique(ZZ, ZZ + N) - ZZ;
  for (int i = 0; i < N; ++i) {
    X[i] = lower_bound(XX, XX + N1, X[i]) - XX;
    Y[i] = lower_bound(YY, YY + N2, Y[i]) - YY;
    Z[i] = lower_bound(ZZ, ZZ + N3, Z[i]) - ZZ;
  }
  for (int i = 0; i < N; ++i) {
    P[Z[i]].push_back(make_pair(make_pair(Y[i], X[i]), i));
  }
  for (int z = 0; z <= 50000; ++z) sort(P[z].begin(), P[z].end());
  for (int i = 0; i < N; ++i) V[i] = 0;
  int n, p1, p2;
  for (int z = 0; z <= 50000; ++z) {
    if (P[z].size() > 0) {
      n = P[z].size();
      p1 = 0;
      while (p1 < n - 1) {
        if (P[z][p1].first.first == P[z][p1 + 1].first.first) {
          cout << P[z][p1].second + 1 << " " << P[z][p1 + 1].second + 1 << endl;
          V[P[z][p1].second] = 1;
          V[P[z][p1 + 1].second] = 1;
          p1 += 2;
        } else {
          ++p1;
        }
      }
      p1 = 0;
      while (p1 < n) {
        while (p1 < n && V[P[z][p1].second] == 1) ++p1;
        if (p1 == n) break;
        p2 = p1 + 1;
        while (p2 < n && V[P[z][p2].second] == 1) ++p2;
        if (p2 == n) break;
        cout << P[z][p1].second + 1 << " " << P[z][p2].second + 1 << endl;
        V[P[z][p1].second] = 1;
        V[P[z][p2].second] = 1;
        p1 = p2 + 1;
      }
    }
  }
  int f = 0;
  int z1;
  for (int z = 0; z <= 50000; ++z)
    for (int p = 0; p < P[z].size(); ++p)
      if (V[P[z][p].second] == 0) {
        if (f == 1) {
          cout << P[z1][p1].second + 1 << " " << P[z][p].second + 1 << endl;
          f = 0;
        } else {
          z1 = z;
          p1 = p;
          ++f;
        }
      }
  return 0;
}
