#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
const int N = 5e5 + 2, inf = 1e9 + 10;
int was[N];
pair<pair<int, int>, pair<int, int>> a[N];
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i)
    cin >> a[i].first.first >> a[i].first.second >> a[i].second.first,
        a[i].second.second = i;
  sort(a, a + n);
  for (int i = 0; i < n - 1; ++i)
    if (a[i].first.first == a[i + 1].first.first &&
        a[i].first.second == a[i + 1].first.second && was[i] == 0 &&
        was[i + 1] == 0) {
      cout << a[i].second.second + 1 << " " << a[i + 1].second.second + 1
           << "\n";
      was[i] = 1;
      was[i + 1] = 1;
    }
  vector<pair<pair<int, int>, pair<int, int>>> a1;
  for (int i = 0; i < n; ++i)
    if (was[i] == 0) a1.push_back(a[i]);
  for (int i = 0; i < n; ++i) was[i] = 0;
  sort(a1.begin(), a1.end());
  for (int i = 0; i < int(a1.size()) - 1; ++i)
    if (a1[i].first.first == a1[i + 1].first.first && was[i] == 0 &&
        was[i + 1] == 0) {
      cout << a1[i].second.second + 1 << " " << a1[i + 1].second.second + 1
           << "\n";
      was[i] = 1;
      was[i + 1] = 1;
    }
  vector<pair<pair<int, int>, pair<int, int>>> a2;
  for (int i = 0; i < a1.size(); ++i)
    if (was[i] == 0) a2.push_back(a1[i]);
  sort(a2.begin(), a2.end());
  for (int i = 0; i < (a2.size()); i += 2)
    cout << a2[i].second.second + 1 << " " << a2[i + 1].second.second + 1
         << "\n";
}
