#include <bits/stdc++.h>
using namespace std;
const int N = 500 + 10;
const long long mod = 1e9 + 7;
struct st {
  long long x;
  long long y;
  long long z;
  long long idx;
} v[60000];
long long n;
bool compare(st a, st b) {
  if (a.x < b.x or (a.x == b.x and a.y < b.y) or
      (a.x == b.x and a.y == b.y and a.z < b.z))
    return 1;
  return 0;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> v[i].x >> v[i].y >> v[i].z;
    v[i].idx = i + 1;
  }
  sort(v, v + n, compare);
  vector<pair<long long, long long> > ans;
  vector<long long> v1, v2;
  long long a = 0;
  for (a = 0; a < n - 1; a++) {
    if (v[a].x == v[a + 1].x && v[a].y == v[a + 1].y) {
      ans.emplace_back(v[a].idx, v[a + 1].idx);
      a++;
      continue;
    }
    v1.emplace_back(a);
  }
  if (a == n - 1) v1.emplace_back(a);
  long long sze = v1.size();
  for (a = 0; a < sze - 1; a++) {
    if (v[v1[a]].x == v[v1[a + 1]].x) {
      ans.emplace_back(v[v1[a]].idx, v[v1[a + 1]].idx);
      a++;
      continue;
    }
    v2.emplace_back(v1[a]);
  }
  if (a == sze - 1) v2.emplace_back(v1[sze - 1]);
  for (long long i = 0; i < v2.size(); i += 2) {
    ans.emplace_back(v[v2[i]].idx, v[v2[i + 1]].idx);
  }
  for (long long i = 0; i < ans.size(); i++)
    cout << ans[i].first << " " << ans[i].second << "\n";
}
