#include <bits/stdc++.h>
using namespace std;
struct pt {
  long long x, y, z, ind;
};
bool operator<(pt a, pt b) {
  if (a.x != b.x) {
    return a.x < b.x;
  }
  if (a.y != b.y) {
    return a.y < b.y;
  }
  return a.z < b.z;
}
pt a[50000];
vector<pair<long long, long long>> ans;
vector<long long> last_glob;
void solve(vector<long long> v) {
  unordered_map<long long, vector<long long>> r;
  set<long long> t;
  for (long long i = 0; i < v.size(); i++) {
    r[a[v[i]].y].push_back(v[i]);
    t.insert(a[v[i]].y);
  }
  vector<long long> last;
  for (auto y : t) {
    if (r[y].size() % 2 != 0) {
      last.push_back(r[y].back());
      r[y].pop_back();
    }
    if (r[y].empty()) {
      continue;
    }
    for (long long i = 0; i < r[y].size() - 1; i += 2) {
      ans.push_back({a[r[y][i]].ind, a[r[y][i + 1]].ind});
    }
  }
  if (last.size() % 2 != 0) {
    last_glob.push_back(last.back());
    last.pop_back();
  }
  if (last.empty()) {
    return;
  }
  for (long long i = 0; i < last.size() - 1; i += 2) {
    ans.push_back({a[last[i]].ind, a[last[i + 1]].ind});
  }
}
signed main() {
  cin.tie(0);
  cout.tie(0);
  ios_base ::sync_with_stdio(0);
  long long n;
  cin >> n;
  for (long long i = 0; i < n; i++) {
    cin >> a[i].x >> a[i].y >> a[i].z;
    a[i].ind = i;
  }
  sort(a, a + n);
  unordered_map<long long, vector<long long>> m;
  set<long long> s;
  for (long long i = 0; i < n; i++) {
    m[a[i].x].push_back(i);
    s.insert(a[i].x);
  }
  for (auto x : s) {
    solve(m[x]);
  }
  if (!last_glob.empty()) {
    for (long long i = 0; i < last_glob.size() - 1; i += 2) {
      ans.push_back({a[last_glob[i]].ind, a[last_glob[i + 1]].ind});
    }
  }
  for (auto p : ans) {
    cout << p.first + 1 << ' ' << p.second + 1 << '\n';
  }
  return 0;
}
