#include <bits/stdc++.h>
using namespace std;
const int DIM = 1e5 + 7;
bool ok[DIM];
struct Point {
  int x, y, z;
  int id;
};
Point v[DIM];
bool cmp(Point A, Point B) {
  if (A.z < B.z) return true;
  if (A.z == B.z) {
    if (A.x < B.x) return true;
    if (A.x == B.x && A.y < B.y) return true;
  }
  return false;
}
vector<int> ramas;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> v[i].x >> v[i].y >> v[i].z;
    v[i].id = i;
  }
  sort(v + 1, v + 1 + n, cmp);
  for (int i = 1; i <= n; i++) {
    int l = i;
    int r = i;
    while (r + 1 <= n && v[r + 1].z == v[i].z) r++;
    i = r;
    vector<int> t;
    for (int j = l; j <= r; j++) {
      if (j + 1 <= r) {
        if (v[j].x == v[j + 1].x) {
          cout << v[j].id << ' ' << v[j + 1].id << '\n';
          j++;
        } else {
          t.push_back(v[j].id);
        }
      } else
        t.push_back(v[j].id);
    }
    for (int j = 1; j < t.size(); j += 2)
      cout << t[j] << ' ' << t[j - 1] << '\n';
    if (int(t.size()) % 2 == 1) ramas.push_back(t.back());
  }
  for (int i = 1; i < ramas.size(); i += 2)
    cout << ramas[i] << ' ' << ramas[i - 1] << '\n';
}
