#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-6;
const int N = 200010;
const int INF = 100000000;
const int mod = 1000000007;
const int dir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, -1, -1, -1, 1, 1, -1, 1, 1};
long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }
long long powmod(long long a, long long b) {
  long long res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1) res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
template <class T>
bool ckmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool ckmax(T& a, const T& b) {
  return a < b ? a = b, 1 : 0;
}
template <class T>
void read(T& x) {
  bool op = 0;
  int c;
  while (c = getchar(), !isdigit(c)) op |= c == '-';
  x = c ^ 48;
  while (c = getchar(), isdigit(c)) x = x * 10 + (c ^ 48);
  if (op) x = -x;
}
template <class A, class... B>
void read(A& a, B&... b) {
  read(a);
  read(b...);
}
template <class T>
void write(T x) {
  if (x < 0) {
    putchar('-');
    x = -x;
  }
  if (x > 9) write(x / 10);
  putchar(x % 10 + 48);
}
long long n;
struct Point {
  long long p[3], id;
} ps[N];
long long ans[N][2];
long long tot;
vector<Point> dc(vector<Point> a, long long d, long long lf, long long rt) {
  if (a.size() <= 1) return a;
  if (lf >= rt) return dc(a, d + 1, -INF, INF);
  long long mid = (lf + rt) >> 1;
  vector<Point> le, ri;
  for (auto const& i : a) {
    if (i.p[d] < mid) {
      le.push_back(i);
    } else {
      ri.push_back(i);
    }
  }
  le = dc(le, d, lf, mid);
  ri = dc(ri, d, mid + 1, rt);
  if (ri.size()) {
    le.push_back(ri[0]);
  }
  if (le.size() > 1) {
    ans[tot][0] = le.back().id;
    le.pop_back();
    ans[tot++][1] = le.back().id;
    le.pop_back();
  }
  return le;
}
void solve() {
  read(n);
  for (int i = 0; i < n; i++) {
    read(ps[i].p[0], ps[i].p[1], ps[i].p[2]);
    ps[i].id = i + 1;
  }
  vector<Point> a;
  for (int i = 0; i < n; i++) {
    a.push_back(ps[i]);
  }
  dc(a, 0, -INF, INF);
  for (int i = 0; i < tot; i++) {
    printf("%lld %lld\n", ans[i][0], ans[i][1]);
  }
}
int main() {
  solve();
  return 0;
}
