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.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/*
4 3
1 2
5 7
8 6

 */
public class C {

	static ArrayList<Integer> res;
	public static void main(String[] args) {
		FS in = new FS();
		PrintWriter out = new PrintWriter(System.out);
		int N = in.nextInt();
		Vec ar[] = new Vec[N];
		for(int i = 0; i < N; i++)
			ar[i] = new Vec(in.nextLong(),in.nextLong(),in.nextLong(),i);
		Arrays.sort(ar, (a,b) -> Long.compare(a.z, b.z));
		res = new ArrayList<Integer>();
		
		int l = 0, r = 0;
		Vec prev = null;
		while(l < N) {
			while(r < N-1 && ar[r+1].z == ar[l].z) r++;
			TreeSet<Vec> ts = new TreeSet<Vec>();
			for(int i = l; i <= r; i++) ts.add(ar[i]);
			if(prev != null) { //find closest and pair
				long best = Long.MAX_VALUE;
				Vec use = null;
				for(Vec v : ts) {
					if(v.man(prev) < best) {
						best = v.man(prev);
						use = v;
					}
				}
				ts.remove(use);
				res.add(use.id); res.add(prev.id);
			}
			prev = solve(ts);
			l = r+1;
			r = l;
		}
		
		for(int i = 0; i < N; i += 2)
			out.println(res.get(i)+" "+res.get(i+1));
		out.close();
	}
	
	static Vec solve(TreeSet<Vec> vs) {
		if(vs.size() == 0) return null;
		if(vs.size() == 1) return vs.first();
		
		TreeMap<Long, TreeSet<Vec>> map = new TreeMap<>();
		for(Vec v : vs) {
			if(!map.containsKey(v.x)) {
				map.put(v.x, new TreeSet<Vec>());
			}
			map.get(v.x).add(v);
		}
		
		for(long ll : map.keySet()) {
			TreeSet<Vec> ts = map.get(ll);
			while(ts.size() > 1) {
				Vec a = ts.pollFirst();
				Vec b = ts.pollFirst();
				res.add(a.id); res.add(b.id);
			}
			if(ts.size() == 1) {
				if(map.higherKey(ll) == null) return ts.first();
				Vec a = ts.pollFirst();
				long l2 = map.higherKey(ll);
				Vec p1 = map.get(l2).ceiling(a);
				Vec p2 = map.get(l2).floor(a);
				Vec use = null;
				if(p1 != null && p2 != null)
					use = Math.abs(p1.y-a.y) < Math.abs(p2.y-a.y) ? p1 : p2;
				else if(p1 != null) use = p1;
				else use = p2;
				
				res.add(a.id); res.add(use.id);
				map.get(l2).remove(use);
			}
		}
		return null;
	}

	static class Vec implements Comparable<Vec>{
		long x,y,z;
		int id;
		public Vec(long xx, long yy, long zz, int ii) {
			x=xx;
			y=yy;
			z=zz;
			id=ii+1;
		}
		@Override
		public int compareTo(Vec o) {
			if(y == o.y) return id-o.id;
			return Long.compare(y, o.y);
		}
		public long man(Vec o) {
			return Math.abs(x-o.x) + Math.abs(y-o.y);
		}
	}
	
	static class FS{
		BufferedReader br;
		StringTokenizer st;
		public FS() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		public String next() {
			while(st == null || !st.hasMoreElements()) {
				try { st = new StringTokenizer(br.readLine());}
				catch(Exception e) { throw null;}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
	}
}
