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 javax.print.attribute.IntegerSyntax;
import java.io.*;
import java.util.*;

/**
 * A simple template for competitive programming problems.
 */
public class Banana {
    final InputReader in = new InputReader(System.in);
    final PrintWriter out = new PrintWriter(System.out);

    final long mod = 1000000009;

    final boolean DEBUG = false;
    boolean[] taken; Point[] points;
    void solve() {
        int n = in.nextInt();
        points = new Point[n];
        for(int i=0; i<n; i++) {
            points[i] = new Point(in.nextInt(), in.nextInt(), in.nextInt(), i+1);
        }
        Arrays.sort(points);
        if (DEBUG) {
            for(int i=0; i<n; i++) {
                out.print(points[i].index + " ");
            }
            out.println();
        }
        taken = new boolean[n];
        for(int i=0; i<n; i++) {
            if(taken[i]) continue;
            int lastJ = i+1;
            while(taken[lastJ]) {
                lastJ++;
            }
            if(points[i].x==points[lastJ].x) {
                if(points[i].y==points[lastJ].y) {
                    print(i, lastJ);
                    continue;
                }
                if(points[i].z <= points[lastJ].z) {
                    print(i, lastJ);
                    continue;
                }
                for(int j=lastJ; j<n; j++) {
                    if(taken[j]) continue;
                    if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y && points[j].z<=points[i].z)) {
                        break;
                    }
                    lastJ = j;
                }
                print(i, lastJ);
            } else {
                if(points[i].y <= points[lastJ].y) {
                    for(int j=lastJ; j<n; j++) {
                        if(taken[j]) continue;
                        if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y && points[j].z<=points[i].z)) {
                            break;
                        }
                        lastJ = j;
                    }
                    print(i, lastJ);
                    continue;
                }
                for(int j=lastJ; j<n; j++) {
                    if(taken[j]) continue;
                    if(!(points[j].x==points[lastJ].x && points[j].y<=points[i].y)) {
                        break;
                    }
                    lastJ = j;
                }
                for(int j=lastJ; j>i; j--) {
                    if(taken[j]) continue;
                    if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y)) {
                        break;
                    }
                    lastJ = j;
                }
                for(int j=lastJ; j<n; j++) {
                    if(taken[j]) continue;
                    if(!(points[j].x==points[lastJ].x && points[j].y==points[lastJ].y && points[j].z<=points[i].z)) {
                        break;
                    }
                    lastJ = j;
                }
                print(i, lastJ);
            }
        }
    }

    private void print(int i, int j) {
        out.println(points[i].index + " " + points[j].index);
        taken[i] = true;
        taken[j] = true;
    }

    private boolean inBetween(Point p1, Point p2, Point p3) {
        int minX = Math.min(p1.x, p2.x);
        int minY = Math.min(p1.y, p2.y);
        int minZ = Math.min(p1.z, p2.z);
        int maxX = Math.max(p1.x, p2.x);
        int maxY = Math.max(p1.y, p2.y);
        int maxZ = Math.max(p1.z, p2.z);

        return p3.x <= maxX && p3.x >= minX && p3.y >= minY && p3.y <= maxY && p3.z <= maxZ && p3.z >= minZ;
    }

    class Point implements Comparable<Point> {
        int x; int y; int z;
        int index;
        Point(int x, int y, int z, int index) {
            this.x = x; this.y = y; this.z = z; this.index = index;
        }
        @Override
        public int compareTo(Point p) {
            int diff = this.x - p.x;
            if(diff!=0)
                return diff;
            diff = this.y - p.y;
            if(diff != 0)
                return diff;
            return this.z - p.z;
        }
    }
    public static void main(String[] args) throws FileNotFoundException { Banana s = new Banana(); s.solve(); s.out.close(); }

    public Banana() throws FileNotFoundException { }

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

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

        InputReader(String fileName) {
            InputStream stream = null;
            try {
                stream = new FileInputStream(fileName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            this.stream = stream;
        }

        int[] nextArray(int n) {
            int[] arr = new int[n];
            for (int i = 0; i < n; i++)
                arr[i] = nextInt();
            return arr;
        }

        int[][] nextMatrix(int n, int m) {
            int[][] matrix = new int[n][m];
            for (int i = 0; i < n; i++)
                for (int j = 0; j < m; j++)
                    matrix[i][j] = nextInt();
            return matrix;
        }

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

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

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

        int nextInt() {
            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;
        }

        double nextDouble() {
            return Double.parseDouble(nextString());
        }

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

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

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


}