#include <bits/stdc++.h>
using namespace std;
template <class T1, class T2>
ostream& operator<<(ostream& out, pair<T1, T2> p) {
  out << '(' << p.first << ',' << p.second << ')';
  return out;
}
template <class T1, class T2>
istream& operator>>(istream& in, pair<T1, T2>& p) {
  in >> p.first >> p.second;
  return in;
}
template <class T>
istream& operator>>(istream& in, vector<T>& v) {
  for (auto& x : v) in >> x;
  return in;
}
template <class T>
ostream& operator<<(ostream& out, vector<vector<T>>& v) {
  for (auto& x : v) out << x << '\n';
  return out;
}
template <class T>
ostream& operator<<(ostream& out, vector<T>& v) {
  for (auto x : v) out << x << ' ';
  return out;
}
long long gcd(long long a, long long b) {
  if (b > a) swap(a, b);
  return (b ? gcd(b, a % b) : a);
}
const long double pi = 2 * asin(1);
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cerr.setstate(ios::failbit);
  int n;
  cin >> n;
  vector<vector<long long>> v(n, vector<long long>(4));
  map<pair<int, int>, vector<pair<int, int>>> m;
  map<int, vector<pair<int, int>>> m2;
  for (register int i = 0; i < n; i++) {
    cin >> v[i][0] >> v[i][1] >> v[i][2];
    v[i][3] = i;
    m[make_pair(v[i][0], v[i][1])].push_back(make_pair(v[i][2], i + 1));
  }
  for (auto& x : m) {
    sort(x.second.begin(), x.second.end());
    while (x.second.size() >= 2) {
      cout << x.second.back().second;
      x.second.pop_back();
      cout << ' ' << x.second.back().second << '\n';
      x.second.pop_back();
    }
    if (x.second.size())
      m2[x.first.first].push_back(
          make_pair(x.first.second, x.second.front().second));
  }
  vector<pair<int, int>> f;
  for (auto& x : m2) {
    sort(x.second.begin(), x.second.end());
    while (x.second.size() >= 2) {
      cout << x.second.back().second;
      x.second.pop_back();
      cout << ' ' << x.second.back().second << '\n';
      x.second.pop_back();
    }
    if (x.second.size())
      f.push_back(make_pair(x.first, x.second.front().second));
  }
  sort(f.begin(), f.end());
  for (int i = 0; i < f.size(); i += 2)
    cout << f[i].second << ' ' << f[i + 1].second << '\n';
}
