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>
import java.util.*;
import java.util.stream.IntStream;
import java.io.*;

public class BalancedRemovalshard {
    
    
    
    /************************ SOLUTION STARTS HERE ************************/
    
    static int pairing[][];
    static int pts[][];
    static int ptr;
    static Integer order[];
    
    static int rec(int dim, int l, int r) {  // r excluded 
        
        if(dim == 0) {  // base case 
            
            for(int i = l; i + 1 < r; i += 2) {
                pairing[ptr][0] = order[i] + 1;
                pairing[ptr][1] = order[i + 1] + 1;
                ptr++;
            }
            
            return (r - l) % 2 == 0 ? -1 : r - 1;
        }
        else {
            
            ArrayList<Integer> collect = new ArrayList<>();
            for(int i = l; i < r; ) {
                int j = i + 1;
                for(; j < r && pts[j][dim] == pts[i][dim]; j++)
                    ;
                
                int ret = rec(dim - 1, i, j);
                i = j;
                if(ret >= 0)
                    collect.add(ret);
            }
            
            for(int i = 0; i + 1 < collect.size(); i += 2) {
                pairing[ptr][0] = order[collect.get(i)] + 1;
                pairing[ptr][1] = order[collect.get(i + 1)] + 1;
                ptr++;
            }
            
            return collect.size() % 2 == 0 ? -1 : collect.get(collect.size() - 1);
        }
    }
    
    private static void solve() {
        
        int n = nextInt();
        pts = new int[n][];
        
        for(int i = 0; i < n; i++) 
            pts[i] = nextIntArray(3);
        
        order = new Integer[n];
        for(int i = 0; i < n; i++)
            order[i] = i;
        
        Arrays.sort(order, (i1, i2) -> pts[i1][2] != pts[i2][2] ? pts[i1][2] - pts[i2][2] 
                                    : (pts[i1][1] != pts[i2][1] ? pts[i1][1] - pts[i2][1] 
                                    :  pts[i1][0] - pts[i2][0]));
        
        Arrays.sort(pts, (p1, p2) -> p1[2] != p2[2] ? p1[2] - p2[2] 
                                    : (p1[1] != p2[1] ? p1[1] - p2[1] : p1[0] - p2[0]));
        
        ptr = 0;
        pairing = new int[n / 2][2];
        
        rec(2, 0, n);
        
        Arrays.stream(pairing).forEach(p -> println(p[0] + " " + p[1]));
        
    }
    
    
    
    /************************ SOLUTION ENDS HERE ************************/
    
    
    
    
    
    /************************ TEMPLATE STARTS HERE **********************/
    
    public static void main(String[] args) throws IOException {
        reader = new BufferedReader(new InputStreamReader(System.in));
        writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
        st     = null;
        solve();
        reader.close();
        writer.close();
    }
    
    static BufferedReader reader;
    static PrintWriter    writer;
    static StringTokenizer st;
    
    static String next()
    {while(st == null || !st.hasMoreTokens()){try{String line = reader.readLine();if(line == null){return null;}            
    st = new StringTokenizer(line);}catch (Exception e){throw new RuntimeException();}}return st.nextToken();}
    static String nextLine()  {String s=null;try{s=reader.readLine();}catch(IOException e){e.printStackTrace();}return s;}             
    static int    nextInt()   {return Integer.parseInt(next());}
    static long   nextLong()  {return Long.parseLong(next());}     
    static double nextDouble(){return Double.parseDouble(next());}
    static char   nextChar()  {return next().charAt(0);}
    static int[]  nextIntArray(int n)         {int[] a= new int[n];   int i=0;while(i<n){a[i++]=nextInt();}  return a;}
    static long[] nextLongArray(int n)        {long[]a= new long[n];  int i=0;while(i<n){a[i++]=nextLong();} return a;}    
    static int[]  nextIntArrayOneBased(int n) {int[] a= new int[n+1]; int i=1;while(i<=n){a[i++]=nextInt();} return a;}            
    static long[] nextLongArrayOneBased(int n){long[]a= new long[n+1];int i=1;while(i<=n){a[i++]=nextLong();}return a;}            
    static void   print(Object o)  { writer.print(o);  }
    static void   println(Object o){ writer.println(o);}
    
    /************************ TEMPLATE ENDS HERE ************************/
    
}