#include <bits/stdc++.h>
using namespace std;
pair<pair<long long, long long>, pair<long long, long long> > x[50505];
vector<pair<long long, long long> > Ans;
long long solve2(long long l, long long r) {
  for (long long i = l; i + 1 <= r; i += 2) {
    Ans.push_back(make_pair(x[i].second.second, x[i + 1].second.second));
  }
  if ((r - l + 1) & 1) {
    return r;
  }
  return 0;
}
long long solve(long long l, long long r) {
  long long i = l, j = l, k = 0;
  for (; i < r; i++) {
    if (x[i].first.second != x[i + 1].first.second) {
      long long u = solve2(j, i);
      j = i + 1;
      if (u == 0) {
        continue;
      }
      if (k == 0) {
        k = u;
      } else {
        Ans.push_back(make_pair(x[k].second.second, x[u].second.second));
        k = 0;
      }
    }
  }
  if (x[r].first.second == x[r - 1].first.second) {
    long long u = solve2(j, r);
    if (u == 0) {
      return k;
    }
    if (k == 0) {
      k = u;
    } else {
      Ans.push_back(make_pair(x[k].second.second, x[u].second.second));
      k = 0;
    }
    return k;
  } else {
    long long u = solve2(j, r - 1);
    if (u == 0) {
      ;
    } else if (k == 0) {
      k = u;
    } else {
      Ans.push_back(make_pair(x[k].second.second, x[u].second.second));
      k = 0;
    }
    u = solve2(r, r);
    if (u == 0) {
      return k;
    }
    if (k == 0) {
      k = u;
    } else {
      Ans.push_back(make_pair(x[k].second.second, x[u].second.second));
      k = 0;
    }
    return k;
  }
}
signed main() {
  cin.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  ;
  long long n;
  cin >> n;
  for (long long i = 1; i <= n; i++) {
    long long a, b, c;
    cin >> x[i].first.first >> x[i].first.second >> x[i].second.first;
    x[i].second.second = i;
  }
  sort(x + 1, x + 1 + n);
  x[n + 1].first.first = 123123123123ll;
  for (long long i = 1, j = 1, k = 0; i <= n; i++) {
    if (x[i].first.first != x[i + 1].first.first) {
      long long u = solve(j, i);
      j = i + 1;
      if (u == 0) {
        continue;
      }
      if (k == 0) {
        k = u;
      } else {
        Ans.push_back(make_pair(x[k].second.second, x[u].second.second));
        k = 0;
      }
    }
  }
  for (auto& a : Ans) {
    cout << a.first << " " << a.second << '\n';
  }
  return 0;
}
