#include <bits/stdc++.h>
#pragma GCC optimize("O3")
using namespace std;
template <typename Arg1>
void __f(const char* name, Arg1&& arg1) {
  cerr << name << " : " << arg1 << std::endl;
}
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 = 1000005;
const long long SQRTN = 1003;
const long long LOGN = 22;
const double PI = acos(-1);
const long long INF = 1e16;
const long long MOD = 1000000007;
const long long FMOD = 998244353;
const double eps = 1e-9;
mt19937 RNG(chrono::steady_clock::now().time_since_epoch().count());
template <typename T>
T gcd(T a, T b) {
  return (b ? __gcd(a, b) : a);
}
template <typename T>
T lcm(T a, T b) {
  return (a * (b / gcd(a, b)));
}
long long add(long long a, long long b, long long c = MOD) {
  long long res = a + b;
  return (res >= c ? res - c : res);
}
long long mod_neg(long long a, long long b, long long c = MOD) {
  long long res;
  if (abs(a - b) < c)
    res = a - b;
  else
    res = (a - b) % c;
  return (res < 0 ? res + c : res);
}
long long mul(long long a, long long b, long long c = MOD) {
  long long res = (long long)a * b;
  return (res >= c ? res % c : res);
}
long long mulmod(long long a, long long b, long long m = MOD) {
  long long q = (long long)(((long double)a * (long double)b) / (long double)m);
  long long r = a * b - q * m;
  if (r > m) r %= m;
  if (r < 0) r += m;
  return r;
}
template <typename T>
T expo(T e, T n) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = x * p;
    p = p * p;
    n >>= 1;
  }
  return x;
}
template <typename T>
T power(T e, T n, T m = MOD) {
  T x = 1, p = e;
  while (n) {
    if (n & 1) x = mul(x, p, m);
    p = mul(p, p, m);
    n >>= 1;
  }
  return x;
}
template <typename T>
T extended_euclid(T a, T b, T& x, T& y) {
  T xx = 0, yy = 1;
  y = 0;
  x = 1;
  while (b) {
    T q = a / b, t = b;
    b = a % b;
    a = t;
    t = xx;
    xx = x - q * xx;
    x = t;
    t = yy;
    yy = y - q * yy;
    y = t;
  }
  return a;
}
template <typename T>
T mod_inverse(T a, T n = MOD) {
  T x, y, z = 0;
  T d = extended_euclid(a, n, x, y);
  return (d > 1 ? -1 : mod_neg(x, z, n));
}
void solvethetestcase();
signed main() {
  cout << fixed << setprecision(10);
  long long t = 1;
  for (long long testcase = 1; testcase < t + 1; testcase++) {
    solvethetestcase();
  }
}
long long n;
pair<pair<long long, long long>, long long> a[50005];
vector<pair<pair<long long, long long>, pair<long long, long long> > > v1;
vector<pair<pair<long long, long long>, pair<long long, long long> > > v2;
vector<pair<pair<long long, long long>, pair<long long, long long> > > v3;
void solvethetestcase() {
  scanf("%lld", &n);
  for (long long i = 0; i < n; i++) {
    scanf("%lld%lld%lld", &a[i].first.first, &a[i].first.second, &a[i].second);
    v1.push_back({{a[i].first.first, a[i].first.second}, {a[i].second, i + 1}});
  }
  sort(v1.begin(), v1.end());
  long long i = 0;
  while (i < v1.size()) {
    if (i != v1.size() - 1 and v1[i].first.first == v1[i + 1].first.first and
        v1[i].first.second == v1[i + 1].first.second) {
      printf("%lld %lld\n", v1[i].second.second, v1[i + 1].second.second);
      i += 2;
    } else {
      v2.push_back(v1[i]);
      i += 1;
    }
  }
  i = 0;
  while (i < v2.size()) {
    if (i != v2.size() - 1 and v2[i].first.first == v2[i + 1].first.first) {
      printf("%lld %lld\n", v2[i].second.second, v2[i + 1].second.second);
      i += 2;
    } else {
      v3.push_back(v2[i]);
      i += 1;
    }
  }
  i = 0;
  while (i < v3.size()) {
    printf("%lld %lld\n", v3[i].second.second, v3[i + 1].second.second);
    i += 2;
  }
}
