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 Main {
	static final long MOD = 998244353;
 
    public static void main(String[] args) throws IOException {
        FastScanner sc = new FastScanner();
        int N = sc.nextInt();
        int[][] points = new int[N][4];
        for (int i = 0; i < N; i++) {
        	points[i][0] = i;
        	for (int j = 1; j <= 3; j++)
        		points[i][j] = sc.nextInt();
        }
        int[][] sorted = sort(points);
        TreeMap<Integer,TreeMap<Integer,TreeSet<Integer>>> tmNext = new TreeMap<Integer,TreeMap<Integer,TreeSet<Integer>>>();
        for (int[] p: points) {
        	if (tmNext.containsKey(p[1])) {
        		TreeMap<Integer,TreeSet<Integer>> tm = tmNext.get(p[1]);
        		if (tm.containsKey(p[2])) {
        			tmNext.get(p[1]).get(p[2]).add(p[3]);
        		} else {
            		TreeSet<Integer> ts = new TreeSet<Integer>();
            		ts.add(p[3]);
        			tm.put(p[2],ts);
        			tmNext.put(p[1],tm);
        		}
        	} else {
        		TreeMap<Integer,TreeSet<Integer>> tm = new TreeMap<Integer,TreeSet<Integer>>();
        		TreeSet<Integer> ts = new TreeSet<Integer>();
        		ts.add(p[3]);
        		tm.put(p[2],ts);
        		tmNext.put(p[1],tm);
        	}
        }
        boolean[] vis = new boolean[N];
        int[][] ans = new int[N/2][2];
        int index = 0;
        HashMap<Point,Integer> hm = new HashMap<Point,Integer>();
        for (int i = 0; i < N; i++) {
        	Point p = new Point(sorted[i][1],sorted[i][2],sorted[i][3]);
        	hm.put(p,sorted[i][0]);
        }
        //System.out.println(Arrays.deepToString(sorted));
        for (int i = 0; i < N; i++) {
        	if (vis[sorted[i][0]]) continue;
        	//Remove point
        	if (tmNext.get(sorted[i][1]).get(sorted[i][2]).size() == 1) {
        		if (tmNext.get(sorted[i][1]).size() == 1)
        			tmNext.remove(sorted[i][1]);
        		else
        			tmNext.get(sorted[i][1]).remove(sorted[i][2]);
        	} else {
        		tmNext.get(sorted[i][1]).get(sorted[i][2]).remove(sorted[i][3]);
        	}
    		TreeMap<Integer,TreeSet<Integer>> tm = new TreeMap<Integer,TreeSet<Integer>>();
        	Integer fx = tmNext.floorKey(sorted[i][1]);
        	Integer cx = tmNext.ceilingKey(sorted[i][1]);
        	int x = 0;
    		if (fx != null) {
        		tm = tmNext.get(fx);
        		x = fx;
        	} else {
        		tm = tmNext.get(cx);
        		x = cx;
        	}
    		TreeSet<Integer> ts = new TreeSet<Integer>();
        	Integer fy = tm.floorKey(sorted[i][2]);
        	Integer cy = tm.ceilingKey(sorted[i][2]);
        	int y = 0;
    		if (fy != null) {
        		ts = tm.get(fy);
        		y = fy;
        	} else {
        		ts = tm.get(cy);
        		y = cy;
        	}
    		
    		int z = 0;
        	Integer fz = ts.floor(sorted[i][3]);
        	Integer cz = ts.ceiling(sorted[i][3]);
        	if (fz != null)
        		z = fz;
        	else
        		z = cz;
        	//Remove used point
        	if (tmNext.get(x).get(y).size() == 1) {
        		if (tmNext.get(x).size() == 1)
        			tmNext.remove(x);
        		else
        			tmNext.get(x).remove(y);
        	} else {
        		tmNext.get(x).get(y).remove(z);
        	}
        	
        	Point p = new Point(x,y,z);
        	int match = hm.get(p);
        	
        	vis[sorted[i][0]] = true;
        	vis[match] = true;
        	ans[index] = new int[]{sorted[i][0]+1,match+1};
        	index++;
        }
        StringBuilder print = new StringBuilder();
        for (int i = 0; i < N/2; i++)
        	print.append(ans[i][0] + " " + ans[i][1] + "\n");
        System.out.print(print);
    }
    
    static class Point {
    	public int x;
    	public int y;
    	public int z;
    	public Point(int x, int y, int z) {
    		this.x = x;
    		this.y = y;
    		this.z = z;
    	}
    	
        @Override
        public int hashCode() {
            long hc = (961 * (long)(x)) + (31 * (long)y) + z;
            hc += 10000*MOD;
            hc %= MOD;
            return (int)hc;
        }
     
        @Override
        public boolean equals(Object obj) {
            if (this == obj)
                return true;
            if (obj == null)
                return false;
            if (getClass() != obj.getClass())
                return false;
            Point other = (Point) obj;
            if (x != other.x || y != other.y || z != other.z)
                return false;
            return true;
        }
    }
    
    public static int[][] sort(int[][] array) {
    	//Sort an array (immune to quicksort TLE)
		Random rgen = new Random();
		for (int i = 0; i < array.length; i++) {
		    int randomPosition = rgen.nextInt(array.length);
		    int[] temp = array[i];
		    array[i] = array[randomPosition];
		    array[randomPosition] = temp;
		}
		Arrays.sort(array, new Comparator<int[]>() {
			  @Override
        	  public int compare(int[] arr1, int[] arr2) {
				  if (arr1[1] != arr2[1])
					  return arr1[1]-arr2[1];
				  else if (arr1[2] != arr2[2])
					  return arr1[2]-arr2[2];
				  else
					  return arr1[3]-arr2[3];
	          }
		});
		return array;
	}
    
    static class FastScanner { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastScanner() { 
            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; 
        }
    }
}