#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using vi = vector<int>;
using vll = vector<ll>;
using pi = pair<int, int>;
using pll = pair<ll, ll>;
int n, a, b, c;
struct p {
  int x;
  int y;
  int z;
  int index;
};
p temp;
vector<p> tab, tab1, tab2, tab3;
bool was[50500];
bool cmp(p a, p b) {
  if (a.x != b.x)
    return a.x < b.x;
  else if (a.y != b.y)
    return a.y < b.y;
  return a.z < b.z;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> a >> b >> c;
    temp.x = a;
    temp.y = b;
    temp.z = c;
    temp.index = i;
    tab.push_back(temp);
  }
  sort((tab).begin(), (tab).end(), cmp);
  for (int i = 0; i < int(tab.size()) - 1; ++i) {
    if (tab[i].x == tab[i + 1].x && tab[i].y == tab[i + 1].y &&
        tab[i].z == tab[i + 1].z) {
      cout << tab[i].index << ' ' << tab[i + 1].index << '\n';
      was[tab[i].index] = true;
      was[tab[i + 1].index] = true;
      ++i;
    }
  }
  for (int i = 0; i < tab.size(); ++i)
    if (!was[tab[i].index]) tab1.push_back(tab[i]);
  for (int i = 0; i < int(tab1.size()) - 1; ++i) {
    if (tab1[i].x == tab1[i + 1].x && tab1[i].y == tab1[i + 1].y) {
      cout << tab1[i].index << ' ' << tab1[i + 1].index << '\n';
      was[tab1[i].index] = true;
      was[tab1[i + 1].index] = true;
      ++i;
    }
  }
  for (int i = 0; i < tab1.size(); ++i)
    if (!was[tab1[i].index]) tab2.push_back(tab1[i]);
  for (int i = 0; i < int(tab2.size()) - 1; ++i) {
    if (tab2[i].x == tab2[i + 1].x) {
      cout << tab2[i].index << ' ' << tab2[i + 1].index << '\n';
      was[tab2[i].index] = true;
      was[tab2[i + 1].index] = true;
      ++i;
    }
  }
  for (int i = 0; i < tab2.size(); ++i)
    if (!was[tab2[i].index]) tab3.push_back(tab2[i]);
  for (int i = 0; i < int(tab3.size()) - 1; i += 2)
    cout << tab3[i].index << ' ' << tab3[i + 1].index << '\n';
}
