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 CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;
const int INF = 999999999;
const double EPSILON = 0.00000001;
const long long MOD = 1000000007;
int p[100005], lv[100005];
int find(int x) {
  if (p[x] != x) p[x] = find(p[x]);
  return p[x];
}
void merge(int x, int y) {
  x = find(x), y = find(y);
  if (lv[x] < lv[y]) swap(x, y);
  p[y] = x;
  if (lv[x] == lv[y]) lv[x]++;
}
struct node {
  int i, val;
  bool col;
};
bool operator<(const node& self, const node& other) {
  return self.val < other.val;
}
struct edge {
  int a, b;
  int val;
};
bool operator<(const edge& self, const edge& other) {
  return self.val < other.val;
}
int main() {
  int n;
  cin >> n;
  vector<node> nodes;
  int w, b;
  for (int i = 0; i < n; i++) {
    node z;
    z.i = i;
    scanf("%d", &z.col);
    scanf("%d", &z.val);
    nodes.push_back(z);
    p[i] = i;
    if (z.col)
      w = i;
    else
      b = i;
  }
  sort(nodes.begin(), nodes.end());
  vector<edge> edges;
  multiset<edge> black, white;
  for (int i = 0; i < n; i++) {
    node h = nodes[i];
    if (h.col == 1) {
      edge e;
      e.a = h.i;
      e.b = -1;
      e.val = h.val;
      multiset<edge>::iterator it = black.begin();
      bool z = true;
      for (; it != black.end(); it++) {
        if (find(it->a) == find(e.a)) continue;
        if (it->val > e.val) {
          edge e2;
          e2.a = it->a;
          e2.b = e.a;
          e2.val = e.val;
          edges.push_back(e2);
          merge(e2.a, e2.b);
          e2 = *it;
          e2.val -= e.val;
          black.erase(black.begin(), ++it);
          black.insert(e2);
          z = false;
          break;
        }
        e.val -= it->val;
        edge e2;
        e2.a = h.i;
        e2.b = it->a;
        e2.val = it->val;
        edges.push_back(e2);
        merge(e2.a, e2.b);
      }
      if (z) {
        white.insert(e);
        int a = black.begin()->a;
        black.erase(black.begin(), it);
      }
    } else {
      edge e;
      e.a = h.i;
      e.b = -1;
      e.val = h.val;
      multiset<edge>::iterator it = white.begin();
      bool z = true;
      for (; it != white.end(); it++) {
        if (find(it->a) == find(e.a)) continue;
        if (it->val > e.val) {
          edge e2;
          e2.a = it->a;
          e2.b = e.a;
          e2.val = e.val;
          edges.push_back(e2);
          merge(e2.a, e2.b);
          e2 = *it;
          e2.val -= e.val;
          white.erase(white.begin(), ++it);
          white.insert(e2);
          z = false;
          break;
        }
        e.val -= it->val;
        edge e2;
        e2.a = h.i;
        e2.b = it->a;
        e2.val = it->val;
        edges.push_back(e2);
        merge(e2.a, e2.b);
      }
      if (z) {
        black.insert(e);
        int a = white.begin()->a;
        white.erase(white.begin(), it);
      }
    }
  }
  for (multiset<edge>::iterator it = black.begin(); it != black.end(); it++) {
    if (find(it->a) != find(w)) {
      edges.push_back((edge){it->a, w, 0});
      merge(it->a, w);
    }
  }
  for (multiset<edge>::iterator it = white.begin(); it != white.end(); it++) {
    if (find(it->a) != find(b)) {
      edges.push_back((edge){it->a, b, 0});
      merge(it->a, b);
    }
  }
  for (vector<edge>::iterator it = edges.begin(); it != edges.end(); it++) {
    printf("%d %d %d\n", it->a + 1, it->b + 1, it->val);
  }
  return 0;
}