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 codeforces {
	public static PrintWriter out;
	public static class MyScanner {
		BufferedReader br;
		StringTokenizer st;
		public MyScanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}
		String next() {
			while (st == null || !st.hasMoreElements()) {
				try {
					st = new StringTokenizer(br.readLine());
				}
				catch (IOException e) {
					e.printStackTrace();
				}
			}
			return st.nextToken();
		}
		int nextInt() {
			return Integer.parseInt(next());
		}
		long nextLong() {
			return Long.parseLong(next());
		}
		double nextDouble() {
			return Double.parseDouble(next());
		}
		String nextLine() {
			String str = "";
			try {
				str = br.readLine();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			return str;
		}
	}
	// The next two void functions are shuffle functions on an array.
	// This way, quick sort encounters the worst case scenario with low probability.
	// A reason for doing this is if we don't a primitive data array to an object array.
	// For safety reasons, it would be preferable to use object arrays unless doing so expends a lot of time.
	public static void shuffleLongArray(long[] arr) {
		int n = arr.length;
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	long tmp = arr[i];
        	int randomPos = i + rnd.nextInt(n-i);
        	arr[i] = arr[randomPos];
        	arr[randomPos] = tmp;
        }
	}
	public static void shuffleIntArray(int[] arr) {
		int n = arr.length;
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	int tmp = arr[i];
        	int randomPos = i + rnd.nextInt(n-i);
        	arr[i] = arr[randomPos];
        	arr[randomPos] = tmp;
        }
	}
	public static void main (String[] args) throws IOException {
    	MyScanner sc = new MyScanner();
    	out = new PrintWriter(new BufferedOutputStream(System.out));
    	// Write your solution here.
    	int n = sc.nextInt();
    	// n is always even
    	double[][] points = new double[n][4];
    	double[][] blast = new double[n][2];
    	double y = Math.pow(2.0, 1.0/3.0);
    	for (int i=0; i < n; i++) {
    		points[i][0] = sc.nextDouble(); points[i][1] = sc.nextDouble(); points[i][2] = sc.nextDouble();
    		double j = (double) i;
    		points[i][3] = j+1.0;
    	}
    	// Shuffle double array
		Random rnd = new Random();
        for (int i=0; i < n; i++) {
        	int randomPos = i + rnd.nextInt(n-i);
        	for (int j=0; j < 4; j++) {
        		double tmp = points[i][j];
        		points[i][j] = points[randomPos][j];
        		points[randomPos][j] = tmp;
        	}
        }
		Arrays.sort(points, new Comparator<double[]>() {
			public int compare (double[] a, double[] b) {
				if (Double.compare(a[0], b[0]) != 0) return Double.compare(a[0], b[0]);
				if (Double.compare(a[1], b[1]) != 0) return Double.compare(a[1], b[1]);
				return Double.compare(a[2], b[2]);
			}
		});
		/*
		for (int i=0; i < n; i++) {
			for (int j=0; j < 4; j++) {
				out.print(points[i][j] + " ");
			}
			out.println();
		}
		*/
		ArrayList<Integer> fiber = new ArrayList<Integer>();
		for (int i=0; i < n; i++) {
			if (i < n-1 && points[i][0] == points[i+1][0] && points[i][1] == points[i+1][1]) {
				int a = (int) points[i][3]; int b = (int) points[i+1][3];
				out.print(a + " " + b);
				out.println();
				i++;
				continue;
			}
			else {
				fiber.add(i);
			}
		}
		double[][] shrink = new double[fiber.size()][4];
		for (int i=0; i < fiber.size(); i++) {
			for (int j=0; j < 4; j++) {
				shrink[i][j] = points[fiber.get(i)][j];
			}
		}
		// shrink is already sorted
		ArrayList<Integer> cell = new ArrayList<Integer>();
		for (int i=0; i < shrink.length; i++) {
			if (i < shrink.length-1 && shrink[i][0] == shrink[i+1][0]) {
				int a = (int) shrink[i][3]; int b = (int) shrink[i+1][3];
				out.print(a + " " + b);
				out.println();
				i++;
				continue;
			}
			else {
				cell.add(i);
			}
		}
		double[][] thanks = new double[cell.size()][4];
		for (int i=0; i < cell.size(); i++) {
			for (int j=0; j < 4; j++) {
				thanks[i][j] = shrink[cell.get(i)][j];
			}
		}
		for (int i=0; i < thanks.length; i++) {
			if (i < thanks.length-1) {
				int a = (int) thanks[i][3]; int b = (int) thanks[i+1][3];
				out.print(a + " " + b);
				out.println();
				i++;
				continue;
			}
			else {
				cell.add(i);
			}
		}
    	out.close();
	}
	
}