#include <bits/stdc++.h>
using namespace std;
mt19937 rng(static_cast<unsigned int>(
    chrono::steady_clock::now().time_since_epoch().count()));
template <typename num_t>
inline void add_mod(num_t& a, const long long& b, const int& m) {
  a = (a + b) % m;
  if (a >= m) a -= m;
  if (a < 0) a += m;
}
template <typename num_t>
inline bool update_max(num_t& a, const num_t& b) {
  return a < b ? a = b, true : false;
}
template <typename num_t>
inline bool update_min(num_t& a, const num_t& b) {
  return a > b ? a = b, true : false;
}
template <typename num_t>
num_t gcd(num_t lhs, num_t rhs) {
  return !lhs ? rhs : gcd(rhs % lhs, lhs);
}
template <typename num_t>
num_t pw(num_t n, num_t k, const num_t& mod) {
  if (k == -1) k = mod - 2;
  num_t res = 1;
  for (; k > 0; k >>= 1) {
    if (k & 1) res = 1ll * res * n % mod;
    n = 1ll * n * n % mod;
  }
  return res % mod;
}
const int inf = 1e9 + 7;
const int mod = 1000000007;
const long long ll_inf = (long long)inf * inf + 7;
const int maxn = 500000 + 7;
void solve() {
  int n;
  cin >> n;
  vector<pair<pair<int, int>, pair<int, int>>> points(n);
  for (auto& p : points) {
    cin >> p.first.first >> p.first.second >> p.second.first;
    static int k = 0;
    p.second.second = ++k;
  }
  sort(points.begin(), points.end());
  vector<int> removed(n, false);
  for (int i = 0; i + 1 < n;) {
    if (points[i].first == points[i + 1].first) {
      cout << points[i].second.second << ' ' << points[i + 1].second.second
           << '\n';
      removed[i] = removed[i + 1] = true;
      i += 2;
    } else {
      i += 1;
    }
  }
  for (int i = 0; i < n; ++i)
    if (!removed[i]) {
      int nxt_i = i + 1;
      while (nxt_i < n && removed[nxt_i]) ++nxt_i;
      if (nxt_i < n && points[i].first.first == points[nxt_i].first.first) {
        cout << points[i].second.second << ' ' << points[nxt_i].second.second
             << '\n';
        removed[i] = removed[nxt_i] = true;
        i = nxt_i;
      } else {
        i = nxt_i - 1;
      }
    }
  vector<int> remain;
  for (int i = 0; i < n; ++i)
    if (!removed[i]) remain.push_back(i);
  for (int i = 0; i < remain.size(); i += 2) {
    int u = remain[i], v = remain[i + 1];
    cout << points[u].second.second << ' ' << points[v].second.second << '\n';
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  const bool multiple_test = false;
  int test = 1;
  if (multiple_test) cin >> test;
  for (int i = 0; i < test; ++i) {
    solve();
  }
}
