#include <bits/stdc++.h>
using namespace std;
const long long MOD2 = 998244353;
const long long MOD = 1e9 + 7;
const long long MAX = 1e5 + 7;
const long long inf = 1e18L + 5;
const double pi = 3.14159265358979323846;
long long fpow(long long n, long long m) {
  long long res = 1;
  while (m > 0) {
    if (m & 1) {
      res *= n;
    }
    m /= 2;
    n *= n;
  }
  return res;
}
const long long nax = 1e5 + 5;
struct point {
  long long x, y, z, i;
};
bool cmp(point s1, point s2) {
  if (s1.x == s2.x) {
    if (s1.y == s2.y) {
      return s1.z < s2.z;
    } else {
      return s1.y < s2.y;
    }
  } else {
    return s1.x < s2.x;
  }
}
void solve() {
  vector<point> v;
  long long a, b, c;
  long long n;
  cin >> n;
  point p;
  for (long long i = 0; i < n; i++) {
    cin >> a >> b >> c;
    p.x = a, p.y = b, p.z = c, p.i = i;
    v.push_back(p);
  }
  sort(v.begin(), v.end(), cmp);
  bool vis[n];
  memset(vis, false, sizeof(vis));
  bool first, sec;
  first = sec = false;
  point pf, ps;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      if (!first) {
        first = true;
        pf = v[i];
        a = i;
      } else if (!sec) {
        sec = true;
        ps = v[i];
        b = i;
      }
    }
    if (first && sec) {
      if (pf.x == ps.x && pf.y == ps.y) {
        cout << pf.i + 1 << " " << ps.i + 1 << "\n";
        first = sec = false;
        vis[a] = vis[b] = true;
      } else {
        a = b;
        pf = ps;
        sec = false;
      }
    }
  }
  first = sec = false;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      if (!first) {
        first = true;
        pf = v[i];
        a = i;
      } else if (!sec) {
        sec = true;
        ps = v[i];
        b = i;
      }
    }
    if (first && sec) {
      if (pf.x == ps.x) {
        cout << pf.i + 1 << " " << ps.i + 1 << "\n";
        first = sec = false;
        vis[a] = vis[b] = true;
      } else {
        a = b;
        pf = ps;
        sec = false;
      }
    }
  }
  first = sec = false;
  for (int i = 0; i < n; ++i) {
    if (!vis[i]) {
      if (!first) {
        first = true;
        pf = v[i];
        a = i;
      } else if (!sec) {
        sec = true;
        ps = v[i];
        b = i;
      }
    }
    if (first && sec) {
      cout << pf.i + 1 << " " << ps.i + 1 << "\n";
      first = sec = false;
      vis[a] = vis[b] = true;
    }
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
