#include <bits/stdc++.h>
using namespace std;
long long int po(long long int x, long long int n) {
  if (n == 0)
    return 1;
  else if (n % 2 == 0)
    return po((x * x) % 1000000007, n / 2);
  else
    return (x * po((x * x) % 1000000007, (n - 1) / 2)) % 1000000007;
}
long long int GCD(long long int A, long long int B) {
  if (B == 0)
    return A;
  else
    return GCD(B, A % B);
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  long long int n;
  cin >> n;
  vector<pair<pair<long long int, long long int>,
              pair<long long int, long long int>>>
      a(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 + 1;
  }
  sort(a.begin(), a.end());
  vector<vector<pair<pair<long long int, long long int>,
                     pair<long long int, long long int>>>>
      b(n);
  vector<pair<pair<long long int, long long int>,
              pair<long long int, long long int>>>
      cc;
  cc.push_back(a[0]);
  for (int i = 1; i < n; ++i) {
    if (a[i].first.first != a[i - 1].first.first) {
      b.push_back(cc);
      cc.clear();
      cc.push_back(a[i]);
    } else
      cc.push_back(a[i]);
  }
  b.push_back(cc);
  cc.clear();
  for (int i = 0; i < b.size(); ++i) {
    if (b[i].size() > 1) {
      vector<vector<pair<pair<long long int, long long int>,
                         pair<long long int, long long int>>>>
          er;
      vector<pair<pair<long long int, long long int>,
                  pair<long long int, long long int>>>
          err;
      err.push_back(b[i][0]);
      for (int j = 1; j < b[i].size(); ++j) {
        if (b[i][j].first.second != b[i][j - 1].first.second) {
          er.push_back(err);
          err.clear();
          err.push_back(b[i][j]);
        } else
          err.push_back(b[i][j]);
      }
      er.push_back(err);
      err.clear();
      for (int j = 0; j < er.size(); ++j) {
        if (er[j].size() == 1)
          err.push_back(er[j][0]);
        else if (er[j].size() > 1) {
          for (int k = 1; k < er[j].size(); k += 2)
            cout << er[j][k].second.second << " " << er[j][k - 1].second.second
                 << "\n";
          if (er[j].size() % 2 == 1) err.push_back(er[j][er[j].size() - 1]);
        }
      }
      for (int j = 1; j < err.size(); j += 2)
        cout << err[j].second.second << " " << err[j - 1].second.second << "\n";
      if (err.size() % 2 == 1) cc.push_back(err[err.size() - 1]);
    } else if (b[i].size() == 1)
      cc.push_back(b[i][0]);
  }
  for (int i = 1; i < cc.size(); i += 2)
    cout << cc[i].second.second << " " << cc[i - 1].second.second << "\n";
}
