#include <bits/stdc++.h>
using namespace std;
template <typename _T>
inline void _DBG(const char *s, _T x) {
  cerr << s << " = " << x << "\n";
}
template <typename _T, typename... args>
void _DBG(const char *s, _T x, args... a) {
  while (*s != ',') cerr << *s++;
  cerr << " = " << x << ',';
  _DBG(s + 1, a...);
}
struct point {
  int x, y, z, id;
};
const int N = 50007;
int n, nx, ny, nz;
point T[N];
map<int, int> MX, MY, MZ;
vector<point> G[N], V[N];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> T[i].x >> T[i].y >> T[i].z;
    T[i].id = i + 1;
    MX[T[i].x] = 1;
    MY[T[i].y] = 1;
    MZ[T[i].z] = 1;
  }
  int cnt = 0;
  for (auto &p : MX) p.second = cnt++;
  cnt = 0;
  for (auto &p : MY) p.second = cnt++;
  cnt = 0;
  for (auto &p : MZ) p.second = cnt++;
  for (int i = 0; i < n; i++) {
    T[i].x = MX[T[i].x];
    T[i].y = MY[T[i].y];
    T[i].z = MZ[T[i].z];
    G[T[i].z].push_back(T[i]);
  }
  nx = (int)(MX).size();
  ny = (int)(MY).size();
  nz = (int)(MZ).size();
  vector<pair<int, int> > ans;
  vector<point> rem;
  for (int i = 0; i < nz; i++) {
    if (G[i].empty()) continue;
    vector<int> ids;
    vector<point> left;
    for (auto p : G[i]) {
      V[p.y].push_back(p);
      ids.push_back(p.y);
    }
    sort((ids).begin(), (ids).end());
    ids.erase(unique((ids).begin(), (ids).end()), ids.end());
    for (auto j : ids) {
      sort((V[j]).begin(), (V[j]).end(),
           [&](const point &p1, const point &p2) { return p1.x < p2.x; });
      for (int k = 1; k < (int)(V[j]).size(); k += 2)
        ans.push_back({V[j][k - 1].id, V[j][k].id});
      if ((int)(V[j]).size() % 2) left.push_back(V[j].back());
      V[j].clear();
    }
    for (int k = 1; k < (int)(left).size(); k += 2)
      ans.push_back({left[k - 1].id, left[k].id});
    if ((int)(left).size() % 2) rem.push_back(left.back());
  }
  assert((int)(rem).size() % 2 == 0);
  for (int k = 1; k < (int)(rem).size(); k += 2)
    ans.push_back({rem[k - 1].id, rem[k].id});
  for (auto [a, b] : ans) cout << a << " " << b << "\n";
  return 0;
}
