#include <bits/stdc++.h>
using namespace std;
mt19937 myrand(chrono::steady_clock::now().time_since_epoch().count());
template <class L, class R>
ostream& operator<<(ostream& os, pair<L, R> P) {
  return os << "(" << P.first << "," << P.second << ")";
}
template <class L, class R, class M>
ostream& operator<<(ostream& os, tuple<L, R, M> P) {
  return os << "(" << get<0>(P) << "," << get<1>(P) << "," << get<2>(P) << ")";
}
template <class T>
ostream& operator<<(ostream& os, vector<T> V) {
  os << "{ ";
  for (auto v : V) os << v << " ";
  return os << "}";
}
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << '\n';
}
template <typename Arg1, typename... Args>
void __f(const char* names, Arg1&& arg1, Args&&... args) {
  const char* comma = strchr(names + 1, ',');
  cerr.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
const long long maxn = (long long)1e5 + 5;
const long double EPS = 1e-9;
const long long INF = (long long)1e18 + 9;
const long long mod = (long long)1e9 + 7;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  long long n, N, T = 0, X, Y, Z;
  cin >> n;
  N = n;
  vector<long long> x(n), y(n), z(n), done(n);
  vector<tuple<long long, long long, long long, long long>> t;
  for (long long i = 0; i < n; i++) {
    cin >> X >> Y >> Z, t.emplace_back(X, Y, Z, i);
  }
  sort(t.begin(), t.end());
  vector<pair<long long, long long>> ans;
  for (long long i = 0; i < n; i++) {
    long long j = i;
    vector<pair<long long, long long>> tmp;
    while (j < n && make_pair(get<0>(t[i]), get<1>(t[i])) ==
                        make_pair(get<0>(t[j]), get<1>(t[j]))) {
      if (done[get<3>(t[j])] == 0) {
        tmp.emplace_back(get<2>(t[j]), get<3>(t[j]));
      }
      j++;
    }
    for (long long k = 0; k < (long long)tmp.size() - 1; k += 2) {
      ans.emplace_back(tmp[k].second, tmp[k + 1].second);
      done[tmp[k].second] = done[tmp[k + 1].second] = 1;
    }
    i = j - 1;
  }
  vector<tuple<long long, long long, long long, long long>> _t;
  for (auto [P, Q, R, S] : t) {
    if (done[S] == 0) {
      _t.emplace_back(P, Q, R, S);
    }
  }
  swap(t, _t);
  n = (long long)t.size();
  for (long long i = 0; i < n; i++) {
    long long j = i;
    vector<pair<long long, long long>> tmp;
    while (j < n && get<0>(t[i]) == get<0>(t[j])) {
      if (done[get<3>(t[j])] == 0) {
        tmp.emplace_back(get<1>(t[j]), get<3>(t[j]));
      }
      j++;
    }
    for (long long k = 0; k < (long long)tmp.size() - 1; k += 2) {
      ans.emplace_back(tmp[k].second, tmp[k + 1].second);
      done[tmp[k].second] = done[tmp[k + 1].second] = 1;
    }
    i = j - 1;
  }
  vector<tuple<long long, long long, long long, long long>> __t;
  for (auto [P, Q, R, S] : t) {
    if (done[S] == 0) {
      __t.emplace_back(P, Q, R, S);
    }
  }
  swap(t, __t);
  vector<long long> rt;
  for (auto [P, Q, R, S] : t) {
    rt.push_back(S);
  }
  for (long long i = 0; i < (long long)rt.size() - 1; i += 2) {
    ans.emplace_back(rt[i], rt[i + 1]);
  }
  for (auto [L, R] : ans) {
    cout << L + 1 << " " << R + 1 << '\n';
  }
  assert((long long)ans.size() == N / 2);
  return 0;
}
