This is a harder version of the problem. In this version, n ≤ 50 000.

There are n distinct points in three-dimensional space numbered from 1 to n. The i-th point has coordinates (x_i, y_i, z_i). The number of points n is even.

You'd like to remove all n points using a sequence of n/2 snaps. In one snap, you can remove any two points a and b that have not been removed yet and form a perfectly balanced pair. A pair of points a and b is perfectly balanced if no other point c (that has not been removed yet) lies within the axis-aligned minimum bounding box of points a and b.

Formally, point c lies within the axis-aligned minimum bounding box of points a and b if and only if min(x_a, x_b) ≤ x_c ≤ max(x_a, x_b), min(y_a, y_b) ≤ y_c ≤ max(y_a, y_b), and min(z_a, z_b) ≤ z_c ≤ max(z_a, z_b). Note that the bounding box might be degenerate. 

Find a way to remove all points in n/2 snaps.

Input

The first line contains a single integer n (2 ≤ n ≤ 50 000; n is even), denoting the number of points.

Each of the next n lines contains three integers x_i, y_i, z_i (-10^8 ≤ x_i, y_i, z_i ≤ 10^8), denoting the coordinates of the i-th point.

No two points coincide.

Output

Output n/2 pairs of integers a_i, b_i (1 ≤ a_i, b_i ≤ n), denoting the indices of points removed on snap i. Every integer between 1 and n, inclusive, must appear in your output exactly once.

We can show that it is always possible to remove all points. If there are many solutions, output any of them.

Examples

Input


6
3 1 0
0 3 0
2 2 0
1 0 0
1 3 0
0 1 0


Output


3 6
5 1
2 4


Input


8
0 1 1
1 0 1
1 1 0
1 1 1
2 2 2
3 2 2
2 3 2
2 2 3


Output


4 5
1 6
2 7
3 8

Note

In the first example, here is what points and their corresponding bounding boxes look like (drawn in two dimensions for simplicity, as all points lie on z = 0 plane). Note that order of removing matters: for example, points 5 and 1 don't form a perfectly balanced pair initially, but they do after point 3 is removed. 

<image>
from __future__ import division, print_function
import bisect
import math
import heapq
import itertools
import sys
from collections import deque
from atexit import register
from collections import Counter
from functools import reduce
sys.setrecursionlimit(10000000)
if sys.version_info[0] < 3:
    from io import BytesIO as stream
else:
    from io import StringIO as stream
 
 
if sys.version_info[0] < 3:
    class dict(dict):
        """dict() -> new empty dictionary"""
        def items(self):
            """D.items() -> a set-like object providing a view on D's items"""
            return dict.iteritems(self)
 
        def keys(self):
            """D.keys() -> a set-like object providing a view on D's keys"""
            return dict.iterkeys(self)
 
        def values(self):
            """D.values() -> an object providing a view on D's values"""
            return dict.itervalues(self)
 
    input = raw_input
    range = xrange
 
    filter = itertools.ifilter
    map = itertools.imap
    zip = itertools.izip
 
 
def sync_with_stdio(sync=True):
    """Set whether the standard Python streams are allowed to buffer their I/O.
 
    Args:
        sync (bool, optional): The new synchronization setting.
 
    """
    global input, flush
 
    if sync:
        flush = sys.stdout.flush
    else:
        sys.stdin = stream(sys.stdin.read())
        input = lambda: sys.stdin.readline().rstrip('\r\n')
 
        sys.stdout = stream()
        register(lambda: sys.__stdout__.write(sys.stdout.getvalue()))
 
def main():
   # from collections import Counter
    n=int(input())
    lol=[]
    d=dict()
    d1=dict()
    for i in range(n):
        x,y,z=map(int,input().split())
        lol.append([x,y,z,i+1])
    lol.sort()
    for ii in range(n):
        x=lol[ii][0]
        y=lol[ii][1]
        z=lol[ii][2]
        i=lol[ii][3]-1
        try:
            d[str([x,y])].append(i+1)
        except:
            d[str([x,y])]=[i+1]
        try:
            d1[x].append(i+1)
        except:
            d1[x]=[i+1]
    vis=[False]*n
    ans=[]
    for i in d.values():
        #i.sort()
        if len(i)>=2:
            for j in range(len(i)//2):
                vis[i[j*2]-1]=True
                vis[i[j*2+1]-1]=True
                ans.append([i[j*2],i[j*2+1]])
    for i in d1.values():
       # i.sort()
        n1=len(i)
        if n1>=2:
            j=0
            k=1
            while k<n1:
                if vis[i[j]-1]==True:
                    j+=1
                if vis[i[k]-1]==True or j==k:
                    k+=1
                if k<n1 and vis[i[k]-1]==False and vis[i[j]-1]==False:
                    ans.append([i[k],i[j]])
                    vis[i[k]-1]=True
                    vis[i[j]-1]=True
    #lol=sorted(lol)
    i=0
    j=1
    c=0
    while j<n:
        if vis[lol[i][3]-1]==True:
            i+=1
        if vis[lol[j][3]-1]==True or j==i:
            j+=1
        if j<n and vis[lol[i][3]-1]==False and vis[lol[j][3]-1]==False:
            ans.append([lol[i][3],lol[j][3]])
            vis[lol[i][3]-1]=True
            vis[lol[j][3]-1]=True
    for i in ans:
        print(*i)
 
 
 
 
if __name__ == '__main__':
    sync_with_stdio(False)
    main()
 

