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.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.util.Random;
import java.util.Comparator;
import java.util.ArrayList;
import java.io.InputStream;

/**
 * @author khokharnikunj8
 */

public class Main {
    public static void main(String[] args) {
        new Thread(null, new Runnable() {
            public void run() {
                new Main().solve();
            }
        }, "1", 1 << 26).start();
    }

    void solve() {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        ScanReader in = new ScanReader(inputStream);
        PrintWriter out = new PrintWriter(outputStream);
        C1BalancedRemovalsEasier solver = new C1BalancedRemovalsEasier();
        solver.solve(1, in, out);
        out.close();
    }

    static class C1BalancedRemovalsEasier {
        public void solve(int testNumber, ScanReader in, PrintWriter out) {
            int n = in.scanInt();
            int[][] ar = new int[n][4];
            for (int i = 0; i < n; i++) {
                for (int j = 0; j < 3; j++) ar[i][j] = in.scanInt();
                ar[i][3] = i + 1;
            }
            CodeHash.shuffle(ar);
            Arrays.sort(ar, new Comparator<int[]>() {

                public int compare(int[] o1, int[] o2) {
                    if (o1[0] == o2[0]) {
                        if (o1[1] == o2[1]) {
                            return o1[2] - o2[2];
                        } else return o1[1] - o2[1];
                    } else return o1[0] - o2[0];
                }
            });

            ArrayList<int[]> ans = new ArrayList<>();
            boolean[] done = new boolean[n];
            for (int i = 1; i < n; i++) {
                boolean check = (!done[i - 1]);
                for (int j = 0; j < 2; j++) {
                    check &= (ar[i][j] == ar[i - 1][j]);
                }
                if (check) {
                    done[i - 1] = true;
                    done[i] = true;
                    ans.add(new int[]{ar[i - 1][3], ar[i][3]});
                }
            }
            ArrayList<int[]> remaining1 = new ArrayList<>();
            for (int i = 0; i < n; i++) {
                if (!done[i]) remaining1.add(ar[i]);
            }
            boolean[] done1 = new boolean[remaining1.size()];
            for (int i = 1; i < remaining1.size(); i++) {
                boolean check = (!done1[i - 1]);
                for (int j = 0; j < 1; j++) {
                    check &= (remaining1.get(i)[j] == remaining1.get(i - 1)[j]);
                }
                if (check) {
                    done1[i - 1] = true;
                    done1[i] = true;
                    ans.add(new int[]{remaining1.get(i - 1)[3], remaining1.get(i)[3]});
                }
            }
            ArrayList<int[]> remaining2 = new ArrayList<>();
            for (int i = 0; i < remaining1.size(); i++) {
                if (!done1[i]) remaining2.add(remaining1.get(i));
            }
            for (int i = 0; i < remaining2.size(); i += 2) {
                ans.add(new int[]{remaining2.get(i)[3], remaining2.get(i + 1)[3]});
            }
            for (int[] p : ans) out.println(p[0] + " " + p[1]);

        }

    }

    static class CodeHash {
        public static void shuffle(int[][] ar) {
            Random rd = new Random(new Random().nextInt());
            for (int i = 0; i < ar.length; i++) {
                int index = rd.nextInt(ar.length);
                int[] temp = ar[i];
                ar[i] = ar[index];
                ar[index] = temp;
            }
        }

    }

    static class ScanReader {
        private byte[] buf = new byte[4 * 1024];
        private int index;
        private BufferedInputStream in;
        private int total;

        public ScanReader(InputStream inputStream) {
            in = new BufferedInputStream(inputStream);
        }

        private int scan() {
            if (index >= total) {
                index = 0;
                try {
                    total = in.read(buf);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (total <= 0) return -1;
            }
            return buf[index++];
        }

        public int scanInt() {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) n = scan();
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                }
            }
            return neg * integer;
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
            else return false;
        }

    }
}

