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.util.*;

public class Main
{
	public static void main(String[] args) {
		new Thread(null, new Runnable() {
			public void run() {
				FastReader fr =new FastReader(System.in);	PrintWriter op =new PrintWriter(System.out);

				int T =1 ;
                
                while (T-->0)
                	solve(fr , op);
                op.flush();	op.close();
            }
        }, "1", 1 << 26).start();
	}
	static void solve (FastReader fr , PrintWriter op) {
 		int N =fr.nextInt() ,i ;
 		ArrayList<triplet> list =new ArrayList<>() ,dummy =new ArrayList<>() ;
		boolean mrk[] =new boolean[N] ;
		triplet A ,B ;

		for (i =0 ; i<N ; ++i)
			list.add(new triplet(i , fr.nextInt() , fr.nextInt() , fr.nextInt())) ;

		for (i =0 ; i<N ; ++i) {
			if (!mrk[list.get(i).id])
				dummy.add(list.get(i)) ;
		}
		Collections.sort(dummy , new cmpXY()) ;
		for (i =0 ; i+1<dummy.size() ; ++i) {
			A =dummy.get(i) ;
			B =dummy.get(i+1) ;
			if (A.X==B.X && A.Y==B.Y) {
				op.println((A.id+1)+" "+(B.id+1)) ;
				mrk[A.id] =mrk[B.id] =true ;
				++i ;
			}
			else
				continue ;
		}
		
		dummy.clear() ;
		for (i =0 ; i<N ; ++i) {
			if (!mrk[list.get(i).id])
				dummy.add(list.get(i)) ;
		}
		Collections.sort(dummy , new cmpZX()) ;
		for (i =0 ; i+1<dummy.size() ; ++i) {
			A =dummy.get(i) ;
			B =dummy.get(i+1) ;
			if (A.X==B.X && A.Z==B.Z) {
				op.println((A.id+1)+" "+(B.id+1)) ;
				mrk[A.id] =mrk[B.id] =true ;
				++i ;
			}
			else
				continue ;
		}

		dummy.clear() ;
		for (i =0 ; i<N ; ++i) {
			if (!mrk[list.get(i).id])
				dummy.add(list.get(i)) ;
		}
		Collections.sort(dummy , new cmpYZ()) ;
		for (i =0 ; i+1<dummy.size() ; ++i) {
			A =dummy.get(i) ;
			B =dummy.get(i+1) ;
			if (A.Y==B.Y && A.Z==B.Z) {
				op.println((A.id+1)+" "+(B.id+1)) ;
				mrk[A.id] =mrk[B.id] =true ;
				++i ;
			}
			else
				continue ;
		}

		dummy.clear() ;
		for (i =0 ; i<N ; ++i) {
			if (!mrk[list.get(i).id])
				dummy.add(list.get(i)) ;
		}
		Collections.sort(dummy , new cmpXY()) ;
		for (i =0 ; i+1<dummy.size() ; ++i) {
			A =dummy.get(i) ;
			B =dummy.get(i+1) ;
			if (A.X==B.X) {
				op.println((A.id+1)+" "+(B.id+1)) ;
				mrk[A.id] =mrk[B.id] =true ;
				++i ;
			}
			else
				continue ;
		}

		dummy.clear() ;
		for (i =0 ; i<N ; ++i) {
			if (!mrk[list.get(i).id])
				dummy.add(list.get(i)) ;
		}
		Collections.sort(dummy , new cmpYZ()) ;
		for (i =0 ; i+1<dummy.size() ; ++i) {
			A =dummy.get(i) ;
			B =dummy.get(i+1) ;
			if (A.Y==B.Y) {
				op.println((A.id+1)+" "+(B.id+1)) ;
				mrk[A.id] =mrk[B.id] =true ;
				++i ;
			}
			else
				continue ;
		}

		dummy.clear() ;
		for (i =0 ; i<N ; ++i) {
			if (!mrk[list.get(i).id])
				dummy.add(list.get(i)) ;
		}
		Collections.sort(dummy , new cmpZX()) ;
		for (i =0 ; i+1<dummy.size() ; ++i) {
			A =dummy.get(i) ;
			B =dummy.get(i+1) ;
			if (A.Z==B.Z) {
				op.println((A.id+1)+" "+(B.id+1)) ;
				mrk[A.id] =mrk[B.id] =true ;
				++i ;
			}
			else
				continue ;
		}

		list.clear() ;
		for (i =0 ; i<dummy.size() ; ++i) {
			if (!mrk[dummy.get(i).id])
				list.add(dummy.get(i)) ;
		}
		for (i =0 ; i+1<list.size() ; ++i) {
			A =list.get(i) ;
			B =list.get(i+1) ;

			op.println((A.id+1)+" "+(B.id+1)) ;
			mrk[A.id] =mrk[B.id] =true ;
			++i ;
		}
	}
	static class FastReader {
        private final InputStream is;
        private final StringBuilder defaultStringBuf = new StringBuilder(1 << 13);
        private final byte[] buf = new byte[1 << 13];
        private int bufLen;
        private int bufOffset;
        private int next;

        public FastReader(InputStream is) {
            this.is = is;
        }

        private int read() {
            while (bufLen == bufOffset) {
                bufOffset = 0;
                try {
                    bufLen = is.read(buf);
                } catch (IOException e) {
                    bufLen = -1;
                }
                if (bufLen == -1) {
                    return -1;
                }
            }
            return buf[bufOffset++];
        }

        public void skipBlank() {
            while (next >= 0 && next <= 32) {
                next = read();
            }
        }

        public String next() {
            return readString();
        }

        public int nextInt() {
            int sign = 1;

            skipBlank();
            if (next == '+' || next == '-') {
                sign = next == '+' ? 1 : -1;
                next = read();
            }

            int val = 0;
            if (sign == 1) {
                while (next >= '0' && next <= '9') {
                    val = val * 10 + next - '0';
                    next = read();
                }
            } else {
                while (next >= '0' && next <= '9') {
                    val = val * 10 - next + '0';
                    next = read();
                }
            }

            return val;
        }

        public long nextLong() {
            return Long.parseLong(readString()) ;
        }

        public String readString(StringBuilder builder) {
            skipBlank();

            while (next > 32) {
                builder.append((char) next);
                next = read();
            }

            return builder.toString();
        }

        public String readString() {
            defaultStringBuf.setLength(0);
            return readString(defaultStringBuf);
        }
    }
}
class triplet {
	int id ,X ,Y ,Z ;

	triplet (int i , int x , int y , int z) {
		id =i ;
		X =x ;
		Y =y ;
		Z =z ;
	}
}
class cmpXY implements Comparator<triplet> {
	@Override
	public int compare (triplet A , triplet B) {
		if (A.X==B.X) {
			if (A.Y==B.Y)
				return A.Z-B.Z ;

			return A.Y-B.Y ;
		}

		return A.X-B.X ;
	}
}
class cmpZX implements Comparator<triplet> {
	@Override
	public int compare (triplet A , triplet B) {
		if (A.Z==B.Z) {
			if (A.X==B.X)
				return A.Y-B.Y ;

			return A.X-B.X ;
		}

		return A.Z-B.Z ;
	}
}
class cmpYZ implements Comparator<triplet> {
	@Override
	public int compare (triplet A , triplet B) {
		if (A.Y==B.Y) {
			if (A.Z==B.Z)
				return A.X-B.X ;

			return A.Z-B.Z ;
		}

		return A.Y-B.Y ;
	}
}