#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int n;
vector<vector<int>> p;
int solve(vector<int> id, int lvl) {
  if (lvl == 3) return id[0];
  map<int, vector<int>> cord;
  for (int i : id) {
    cord[p[i][lvl]].push_back(i);
  }
  vector<int> rem;
  for (auto m : cord) {
    int cur = solve(m.second, lvl + 1);
    if (cur != -1) {
      rem.push_back(cur);
    }
  }
  for (int i = 0; i < (int)rem.size() - 1; i++) {
    cout << rem[i] + 1 << ' ' << rem[i + 1] + 1 << '\n';
    i++;
  }
  if ((int)rem.size() % 2 == 1)
    return rem.back();
  else
    return -1;
}
int main(void) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  p.resize(n, vector<int>(3));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < 3; j++) cin >> p[i][j];
  vector<int> id(n);
  iota(id.begin(), id.end(), 0);
  solve(id, 0);
  return 0;
}
