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.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
 
 
 public class q5 {
	
	 	
	public static void main(String[] args) throws IOException {
	
		Reader.init(System.in);
		PrintWriter out=new PrintWriter(System.out);
		int n=Reader.nextInt();
		TreeMap<Integer,NoD> xmap=new TreeMap<Integer,NoD>();
		TreeMap<Integer,TreeSet<Integer>> count=new TreeMap<Integer,TreeSet<Integer>>();
		TreeMap<Integer,Integer> c2=new TreeMap<Integer,Integer>();
		for(int i=0;i<n;i++) {
			int x=Reader.nextInt(),y=Reader.nextInt(),z=Reader.nextInt();
			if(!c2.containsKey(x)) c2.put(x, 0);
			int v=c2.get(x);
			if(v!=0) {
				count.get(v).remove(x);
				if(count.get(v).size()==0) count.remove(v);
			}
			v++;
			c2.put(x, v);
			if(!count.containsKey(v)) count.put(v, new TreeSet<Integer>());
			count.get(v).add(x);
			if(!xmap.containsKey(x)) {
				xmap.put(x,new NoD());
			}
			xmap.get(x).insert(y, z, i+1);
			
		}
		for(int i=0;i<n/2;i++) {
			int a=count.lastKey();
			if(a==1) {
				int x1=count.get(a).pollFirst();
				int x2=count.get(a).pollFirst();
				int i1=xmap.get(x1).get2(),i2=xmap.get(x2).get2();
				out.println(i1+" "+i2);
			}
			else {
				int x=count.get(a).pollFirst();
				if(count.get(a).size()==0) count.remove(a);
				N3 ans=xmap.get(x).get();
				out.println(ans.i1+" "+ans.i2);
				a-=2;
				if(a>0) {
					if(!count.containsKey(a)) count.put(a, new TreeSet<Integer>());
					count.get(a).add(x);
				}
			}
		}
		
		
		out.flush();
	}	
}
 class N3{
	 int i1, i2;
	 N3(int ii1, int ii2){
		 i1=ii1;i2=ii2;
	 }
 }
class NoD{
	TreeMap<Integer,N2> tm;
	TreeMap<Integer,TreeSet<Integer>> count;
	TreeMap<Integer,Integer> c2;
	NoD(){
		tm=new TreeMap<Integer,N2>();
		count=new TreeMap<Integer,TreeSet<Integer>>();
		c2=new TreeMap<Integer,Integer>();
	}
	void insert(int y, int z, int ind) {
		if(!c2.containsKey(y)) {
			c2.put(y, 0);
		}
		int v=c2.get(y);
		if(v!=0) {
			count.get(v).remove(y);
			if(count.get(v).size()==0) count.remove(v);
		}
		v++;
		c2.put(y, v);
		if(!count.containsKey(v)) count.put(v, new TreeSet<Integer>());
		count.get(v).add(y);
		if(!tm.containsKey(y)) tm.put(y, new N2());
		tm.get(y).insert(z, ind);
	}
	
	N3 get() {
		int a=count.lastKey();
		if(a==1) {
			int y1=count.get(a).pollFirst();
			int y2=count.get(a).pollFirst();
			int i1=tm.get(y1).get(),i2=tm.get(y2).get();
			return new N3(i1,i2);
		}
		else {
			int y=count.get(a).pollFirst();
			if(count.get(a).size()==0) count.remove(a);
			int i1=tm.get(y).get();
			int i2=tm.get(y).get();
			a-=2;
			if(a>0) {
				if(!count.containsKey(a))count.put(a, new TreeSet<Integer>());
				count.get(a).add(y);
			}
			return new N3(i1,i2);
		}
		
	}
	int get2() {
		int y=count.get(1).pollFirst();
		int a=tm.get(y).get();
		tm.remove(y);
		return a;
	}
}

class N2
{
	TreeMap<Integer,Integer> tm;
	N2(){
		tm=new TreeMap<Integer,Integer>();
	}
	void insert(int z, int y) {
		tm.put(z, y);
	}
	
	int get() {
		int z=tm.lastKey();
	
		int c= tm.get(z);
		tm.remove(z);
		return c;
	}
}


 
 
class Reader {
    static BufferedReader reader;
    static StringTokenizer tokenizer;
    /** call this method to initialize reader for InputStream */
    static void init() throws IOException {
    	 reader = new BufferedReader(
                 new FileReader("input.txt"));
    tokenizer = new StringTokenizer("");
    }
    static void init(InputStream input) {
        reader = new BufferedReader(
                     new InputStreamReader(input) );
        tokenizer = new StringTokenizer("");
    }
    /** get next word */
    static String nextLine() throws IOException{
    	return reader.readLine();
    }
    static String next() throws IOException {
        while ( ! tokenizer.hasMoreTokens() ) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                   reader.readLine() );
        }
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException {
        return Integer.parseInt( next() );
    }
    static long nextLong() throws IOException {
        return Long.parseLong( next() );
    }
    static double nextDouble() throws IOException {
        return Double.parseDouble( next() );
    }
}