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.OutputStream;
import java.io.PrintWriter;
import java.util.PriorityQueue;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.AbstractCollection;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;

/**
 * Built using CHelper plug-in
 * Actual solution is at the top
 *
 * @author Say My Name
 */
public class Main {
    public static void main(String[] args) {
        InputStream inputStream = System.in;
        OutputStream outputStream = System.out;
        InputReader in = new InputReader(inputStream);
        OutputWriter out = new OutputWriter(outputStream);
        C1BalancedRemovalsEasier solver = new C1BalancedRemovalsEasier();
        solver.solve(1, in, out);
        out.close();
    }

    static class C1BalancedRemovalsEasier {
        public void solve(int testNumber, InputReader c, OutputWriter w) {
            int n = c.readInt();
            PriorityQueue<Point> pq = new PriorityQueue<>();
            for (int i = 0; i < n; ++i) {
                pq.add(new Point(c.readInt(), c.readInt(), c.readInt(), i + 1));
            }
            PriorityQueue<Point> pq1 = new PriorityQueue<>();
            while (!pq.isEmpty()) {
                Point p1 = pq.poll();
                if (!pq.isEmpty()) {
                    Point p2 = pq.poll();
                    if (comp1(p1, p2)) {
                        w.printLine(p1.ind + " " + p2.ind);
                    } else {
                        pq1.add(p1);
                        pq.add(p2);
                    }
                } else {
                    pq1.add(p1);
                }
            }
            PriorityQueue<Point> pq2 = new PriorityQueue<>();
            while (!pq1.isEmpty()) {
                Point p1 = pq1.poll();
                if (!pq1.isEmpty()) {
                    Point p2 = pq1.poll();
                    if (comp2(p1, p2)) {
                        w.printLine(p1.ind + " " + p2.ind);
                    } else {
                        pq2.add(p1);
                        pq1.add(p2);
                    }
                } else {
                    pq2.add(p1);
                }
            }

            while (!pq2.isEmpty()) {
                w.printLine(pq2.poll().ind + " " + pq2.poll().ind);
            }

        }

        private boolean comp2(Point p1, Point p2) {
            return p1.x == p2.x;
        }

        private boolean comp1(Point p1, Point p2) {
            return p1.x == p2.x && p1.y == p2.y;
        }

    }

    static class Point implements Comparable<Point> {
        int x;
        int y;
        int z;
        int ind;

        public Point(int x, int y, int z, int ind) {
            this.x = x;
            this.y = y;
            this.z = z;
            this.ind = ind;
        }

        public Point(int x, int y, int z) {
            this.x = x;
            this.y = y;
            this.z = z;
        }

        public int compareTo(Point o) {
            if (this.x == o.x) {
                if (this.y == o.y) {
                    return this.z - o.z;
                }
                return this.y - o.y;
            }
            return this.x - o.x;
        }

    }

    static class OutputWriter {
        private final PrintWriter writer;

        public OutputWriter(OutputStream outputStream) {
            writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
        }

        public OutputWriter(Writer writer) {
            this.writer = new PrintWriter(writer);
        }

        public void print(Object... objects) {
            for (int i = 0; i < objects.length; i++) {
                if (i != 0) {
                    writer.print(' ');
                }
                writer.print(objects[i]);
            }
        }

        public void printLine(Object... objects) {
            print(objects);
            writer.println();
        }

        public void close() {
            writer.close();
        }

    }

    static class InputReader {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;
        private InputReader.SpaceCharFilter filter;

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

        public 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++];
        }

        public int readInt() {
            int c = read();
            while (isSpaceChar(c)) {
                c = read();
            }
            int sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = read();
            }
            int res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = read();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public boolean isSpaceChar(int c) {
            if (filter != null) {
                return filter.isSpaceChar(c);
            }
            return isWhitespace(c);
        }

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

        public interface SpaceCharFilter {
            public boolean isSpaceChar(int ch);

        }

    }
}

