Problem: The board has got a painted tree graph, consisting of n nodes. Let us remind you that a non-directed graph is called a tree if it is connected and doesn't contain any cycles.

Each node of the graph is painted black or white in such a manner that there aren't two nodes of the same color, connected by an edge. Each edge contains its value written on it as a non-negative integer.

A bad boy Vasya came up to the board and wrote number sv near each node v — the sum of values of all edges that are incident to this node. Then Vasya removed the edges and their values from the board.

Your task is to restore the original tree by the node colors and numbers sv.

Input

The first line of the input contains a single integer n (2 ≤ n ≤ 105) — the number of nodes in the tree. Next n lines contain pairs of space-separated integers ci, si (0 ≤ ci ≤ 1, 0 ≤ si ≤ 109), where ci stands for the color of the i-th vertex (0 is for white, 1 is for black), and si represents the sum of values of the edges that are incident to the i-th vertex of the tree that is painted on the board.

Output

Print the description of n - 1 edges of the tree graph. Each description is a group of three integers vi, ui, wi (1 ≤ vi, ui ≤ n, vi ≠ ui, 0 ≤ wi ≤ 109), where vi and ui — are the numbers of the nodes that are connected by the i-th edge, and wi is its value. Note that the following condition must fulfill cvi ≠ cui.

It is guaranteed that for any input data there exists at least one graph that meets these data. If there are multiple solutions, print any of them. You are allowed to print the edges in any order. As you print the numbers, separate them with spaces.

Examples

Input

3
1 3
1 2
0 5


Output

3 1 3
3 2 2


Input

6
1 0
0 3
1 8
0 2
0 3
0 0


Output

2 3 3
5 3 3
4 3 2
1 6 0
2 1 0

Solution (in JAVA):

Solution:
import java.util.*;
 
 
import static java.lang.Math.*;
 
import java.io.DataInputStream; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger; 
 
public class Main 
{ 
 
	static int mod = 1000000007;
	static long MOD = 1000000007;
	static long temp = 998244353L;
	static final long M = (int)1e9+7;
 
	
	static class Pair implements Comparable<Pair>
	{
		int first, second;
		public Pair(int aa, int bb)
		{
			first = aa; second = bb;
		}
		public int compareTo(Pair p)
		{
//			if(a == p.a) return b - p.b;
//			return a - p.a;
			if(first == p.first) return (second - p.second);
			return (first - p.first);
		}
	}
	
	/*
	 * IO FOR 2D GRID IN JAVA
	 * char[][] arr = new char[n][m]; //grid in Q.
			
			for(int i = 0;i<n;i++)
			{
				char[] nowLine = sc.next().toCharArray();
				for(int j = 0;j<m;j++)
				{
					arr[i][j] = nowLine[i];
				}
			}
	 * */
	
	static class Reader 
	{ 
		final private int BUFFER_SIZE = 1 << 16; 
		private DataInputStream din; 
		private byte[] buffer; 
		private int bufferPointer, bytesRead; 
 
		public Reader() 
		{ 
			din = new DataInputStream(System.in); 
			buffer = new byte[BUFFER_SIZE]; 
			bufferPointer = bytesRead = 0; 
		} 
 
		public Reader(String file_name) throws IOException 
		{ 
			din = new DataInputStream(new FileInputStream(file_name)); 
			buffer = new byte[BUFFER_SIZE]; 
			bufferPointer = bytesRead = 0; 
		} 
 
		
		public String next() throws IOException 
		{ 
			byte[] buf = new byte[64]; // line length 
			int cnt = 0, c; 
			while ((c = read()) != -1) 
			{ 
				if (c == '\n') 
					break; 
				buf[cnt++] = (byte) c; 
			} 
			return new String(buf, 0, cnt); 
		} 
	
		
		public int nextInt() throws IOException 
		{ 
			int ret = 0; 
			byte c = read(); 
			while (c <= ' ') 
				c = read(); 
			boolean neg = (c == '-'); 
			if (neg) 
				c = read(); 
			do
			{ 
				ret = ret * 10 + c - '0'; 
			} while ((c = read()) >= '0' && c <= '9'); 
 
			if (neg) 
				return -ret; 
			return ret; 
		}
		
		int[] readArray(int n) throws IOException {
			int[] a=new int[n];
			for (int i=0; i<n; i++) a[i]=nextInt();
			return a;
		}
		
		long[] longReadArray(int n) throws IOException {
			long[] a=new long[n];
			for (int i=0; i<n; i++) a[i]=nextLong();
			return a;
		}
 
		public long nextLong() throws IOException 
		{ 
			long ret = 0; 
			byte c = read(); 
			while (c <= ' ') 
				c = read(); 
			boolean neg = (c == '-'); 
			if (neg) 
				c = read(); 
			do { 
				ret = ret * 10 + c - '0'; 
			} 
			while ((c = read()) >= '0' && c <= '9'); 
			if (neg) 
				return -ret; 
			return ret; 
		} 
 
		public double nextDouble() throws IOException 
		{ 
			double ret = 0, div = 1; 
			byte c = read(); 
			while (c <= ' ') 
				c = read(); 
			boolean neg = (c == '-'); 
			if (neg) 
				c = read(); 
 
			do { 
				ret = ret * 10 + c - '0'; 
			} 
			while ((c = read()) >= '0' && c <= '9'); 
 
			if (c == '.') 
			{ 
				while ((c = read()) >= '0' && c <= '9') 
				{ 
					ret += (c - '0') / (div *= 10); 
				} 
			} 
 
			if (neg) 
				return -ret; 
			return ret; 
		} 
 
		private void fillBuffer() throws IOException 
		{ 
			bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE); 
			if (bytesRead == -1) 
				buffer[0] = -1; 
		} 
 
		private byte read() throws IOException 
		{ 
			if (bufferPointer == bytesRead) 
				fillBuffer(); 
			return buffer[bufferPointer++]; 
		} 
 
		public void close() throws IOException 
		{ 
			if (din == null) 
				return; 
			din.close(); 
		}
 
		public BigInteger nextBigInteger() {
			// TODO Auto-generated method stub
			return null;
		}
 
	}
 
	
	public static boolean isPrime(long n) {
		if(n == 1)
		{
			return false;
		}
		for(long i = 2;i*i<=n;i++)
		{
			if(n%i == 0)
			{
				return false;
			}
		}
		return true;
	}
	
	public static List<Integer> Sieve(int n)
	{
		boolean prime[] = new boolean[n+1];
		Arrays.fill(prime, true);
		List<Integer> l = new ArrayList<>();
		
		
		for (int p=2; p*p<=n; p++) 
		{ 
			if (prime[p] == true) 
			{ 	
			    for(int i=p*p; i<=n; i += p) 
				{
				    prime[i] = false; 
				}	
			} 
		} 
 
		for (int p=2; p<=n; p++) 
		{
		    if (prime[p] == true)
		    {
		       l.add(p); 
		    }
		}
		
		return l;
	}
	
	
	public static int gcd(int a, int b)
	{
		if(b == 0)
		 return a;
		
		else
		return gcd(b,a%b);
	}
	
	public static long LongGCD(long a, long b)
	{
		if(b == 0)
			 return a;
			
			else
			return LongGCD(b,a%b);
	}
 
 
	public static int lcm(int a, int b)
    {
        return (a / gcd(a, b)) * b;
    }
	
	public static int phi(int n)  //euler totient function
    { 
        int result = 1; 
        for (int i = 2; i < n; i++) 
            if (gcd(i, n) == 1) 
                result++; 
        return result; 
    }
	
	    
	public static int[] computePrefix(int arr[], int n)
	{
		int[] prefix = new int[n];
		prefix[0] = arr[0];
		for(int i = 1;i<n;i++)
		{
			prefix[i] = prefix[i-1]+arr[i];
		}
		
		return prefix;
	}
	
	
	public static long fastPow(long x, long n) //include mod at each step if asked and in args of fn too
	{
		if(n == 0)
			return 1;
		else if(n%2 == 0)
			return fastPow(x*x,n/2);
		else
			return x*fastPow(x*x,(n-1)/2);
	}
	
	
	   public static long power(long x, long y, long p)
	    {
	 
	        long res = 1;
	 
	        x = x % p;
	 
	        while (y > 0) {
	            if (y % 2 == 1)
	                res = (res * x) % p;
	 
	            y = y >> 1; 
	            x = (x * x) % p;
	        }
	 
	        return res;
	    }
	   
	   static long modInverse(long n, long p)
	    {
	        return power(n, p - 2, p);
	    }
	 
	    // Returns nCr % p using Fermat's little theorem.
	    
	    public static long nCr(long n, long r,
	                             long p)
	    {
	          if (n<r) 
	              return 0;
	          
	        if (r == 0)
	            return 1;
	 
	        long[] fac = new long[(int)(n) + 1];
	        fac[0] = 1;
	 
	        for (int i = 1; i <= n; i++)
	            fac[i] = fac[i - 1] * i % p;
	 
	        return (fac[(int)(n)] * modInverse(fac[(int)(r)], p)
	                % p * modInverse(fac[(int)(n - r)], p)
	                % p)
	            % p;
	    }
	    
	    public static long[] modInvArr(long n, long mod)
	    {
	    	long[] ans = new long[(int)(n)+1];
			ans[1] = 1;
			for(int i = 2;i <= n;i++)
			{
				ans[i] = (mod - (mod/i) * ans[(int)(mod%i)])%mod;
			}
			return ans;
	    }
 
		public static int LowerBound(int a[], int x) {
			  int l=-1,r=a.length;
			  while(l+1<r) {
			    int m=(l+r)>>>1;
			    if(a[m]>=x) r=m;
			    else l=m;
			  }
			  return r;
			}
		
		
		public static int UpperBound(int a[], int x) {
		    int l=-1;
		    int r=a.length;
		    while(l+1<r) {
		       int m=(l+r)>>>1;
		       if(a[m]<=x) l=m;
		       else r=m;
		    }
		    return l+1;
		}
		
		public static void Sort(int[] a) {
			List<Integer> l=new ArrayList<>();
			for (int i:a) l.add(i);
			Collections.sort(l);
			//Collections.reverse(l);  //Use to Sort decreasingly 
			for (int i=0; i<a.length; i++) a[i]=l.get(i);
		}
		
		//MODULO OPS for addition and multiplication 
		
		   public static long perfomMod(long x){
		        return ((x%M + M)%M);
		    }
		   public static long addMod(long a, long  b){
		        return perfomMod(perfomMod(a)+perfomMod(b));
		    }
		   public static long mulMod(long  a, long b){
		        return perfomMod(perfomMod(a)*perfomMod(b));
		   }
		
		   public static class TreeNode
		   {
			   int weight;
			   int val;
			   public TreeNode(int val)
			   {
				   this.val = val;
			   }
		   }
		   
		public static void main(String[] args) throws IOException 
		{ 
			Reader sc = new Reader();
			PrintWriter out = new PrintWriter(System.out);
			
			
				int n = sc.nextInt();
				ArrayList<TreeNode> b = new ArrayList<>();
				ArrayList<TreeNode> w = new ArrayList<>();
				
				for(int i = 0;i<n;i++)
				{
					int color = sc.nextInt();
					int sum = sc.nextInt();
					TreeNode curr = new TreeNode(i);
					curr.weight = sum;
					if(color == 0)
					{
						b.add(curr);
					}
					else {
						w.add(curr);
					}
				}
				
				int left = 0, right = 0;
				while(left < b.size() && right < w.size())
				{
					TreeNode kuro = b.get(left);
					TreeNode shiroi = w.get(right);
					int wei = min(kuro.weight,shiroi.weight);
					System.out.println(kuro.val + 1 + " " +(shiroi.val + 1) + " " + wei);
					kuro.weight -= wei;
					shiroi.weight -= wei;
					if(kuro.weight < shiroi.weight)
					{
						left++;
					}
					else {
						right++;
					}
				}
				
				while(left < b.size() - 1)
				{
					left++;
					TreeNode one = b.get(left);
					TreeNode two = w.get(right-1);
					int wei = min(one.weight,two.weight);
					System.out.println(one.val + 1 + " " +(two.val + 1) + " " + wei);
				}
				
				while(right < w.size() - 1)
				{
					right++;
					TreeNode one = b.get(left-1);
					TreeNode two = w.get(right);
					
					int wei = min(one.weight,two.weight);
					System.out.println(one.val + 1 + " " +(two.val + 1) + " " + wei);
				}
			
			out.close();
		}
}