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.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.nio.charset.StandardCharsets;
import java.util.ArrayDeque;
import java.util.InputMismatchException;
import java.util.TreeMap;

public class c3po {

	static class Solver {
		
		int N, ans[][], AID = 0;
		TreeMap<Integer, TreeMap<Integer, TreeMap<Integer, Integer>>> pts;
		
		// all points have same x and y; solve
		int solveY(TreeMap<Integer, Integer> ypts) {
			int last = -1;
			for(int z : ypts.keySet()) {
				z = ypts.get(z);
				if(last != -1) {
					ans[AID][0] = last;
					ans[AID++][1] = z;
					last = -1;
				} else
					last = z;
			}
			return last;
		}
		
		// all points have same x; solve
		int solveX(TreeMap<Integer, TreeMap<Integer, Integer>> xpts) {
			ArrayDeque<Integer> post = new ArrayDeque<>();
			for(int y : xpts.keySet()) {
				int go = solveY(xpts.get(y));
				if(go != -1) post.add(go);
			}
			while(!post.isEmpty()) {
				if(post.size() == 1) return post.pollFirst();
				ans[AID][0] = post.pollFirst();
				ans[AID++][1] = post.pollFirst();
			}
			return -1;
		}
		
		void add(int x, int y, int z, int id) {
			if (!pts.containsKey(x)) pts.put(x, new TreeMap<>());
			if (!pts.get(x).containsKey(y)) pts.get(x).put(y, new TreeMap<>());
			pts.get(x).get(y).put(z, id);
		}
		
		void solve(FastScanner s, PrintWriter out) {
			N = s.nextInt(); pts = new TreeMap<>();
			for(int x, y, z, i = 0; i < N; i++) {
				x = s.nextInt(); y = s.nextInt(); z = s.nextInt();
				add(x, y, z, i + 1);
			}
			
			ans = new int[N / 2][2];
			ArrayDeque<Integer> post = new ArrayDeque<>();
			for(int x : pts.keySet()) {
				int go = solveX(pts.get(x));
				if (go != -1) post.add(go);
			}
			while(!post.isEmpty()) {
				ans[AID][0] = post.pollFirst();
				ans[AID++][1] = post.pollFirst();
			}
			
			for(int[] a : ans) 
				out.printf("%d %d%n", a[0], a[1]);
		}
	}
	
	public static void main(String[] args) {
		
		FastScanner s = new FastScanner(System.in);
		PrintWriter out = new PrintWriter(System.out);
		
		Solver solver = new Solver();
		solver.solve(s, out);
		
		out.close();
		s.close();
		
	}
	
	static class FastScanner {
		private InputStream stream;
		private byte[] buf = new byte[1024];
		private int curChar;
		private int numChars;

		public FastScanner(InputStream stream) {
			this.stream = stream;
		}

		public FastScanner(File f) throws FileNotFoundException {
			this(new FileInputStream(f));
		}

		public FastScanner(String s) {
			this.stream = new ByteArrayInputStream(s.getBytes(StandardCharsets.UTF_8));
		}
		
		void close() {
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		int read() {
			if (numChars == -1)
				throw new InputMismatchException();
			if (curChar >= numChars) {
				curChar = 0;
				try {
					numChars = stream.read(buf);
				} catch (IOException e) {
					throw new InputMismatchException();
				}
				if (numChars <= 0)
					return -1;
			}
			return buf[curChar++];
		}

		boolean isSpaceChar(int c) {
			return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
		}

		boolean isEndline(int c) {
			return c == '\n' || c == '\r' || c == -1;
		}

		public int nextInt() {
			return Integer.parseInt(next());
		}

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

		public double nextDouble() {
			return Double.parseDouble(next());
		}

		public String next() {
			int c = read();
			while (isSpaceChar(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isSpaceChar(c));
			return res.toString();
		}

		public String nextLine() {
			int c = read();
			while (isEndline(c))
				c = read();
			StringBuilder res = new StringBuilder();
			do {
				res.appendCodePoint(c);
				c = read();
			} while (!isEndline(c));
			return res.toString();
		}

		// Jacob Garbage

		public int[] nextIntArray(int N) {
			int[] ret = new int[N];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextInt();
			return ret;
		}

		public int[][] next2DIntArray(int N, int M) {
			int[][] ret = new int[N][];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextIntArray(M);
			return ret;
		}

		public long[] nextLongArray(int N) {
			long[] ret = new long[N];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextLong();
			return ret;
		}

		public long[][] next2DLongArray(int N, int M) {
			long[][] ret = new long[N][];
			for (int i = 0; i < N; i++)
				ret[i] = nextLongArray(M);
			return ret;
		}

		public double[] nextDoubleArray(int N) {
			double[] ret = new double[N];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextDouble();
			return ret;
		}

		public double[][] next2DDoubleArray(int N, int M) {
			double[][] ret = new double[N][];
			for (int i = 0; i < N; i++)
				ret[i] = this.nextDoubleArray(M);
			return ret;
		}

	}

	
}
