Problem: Pieguy and Piegirl are playing a game. They have a rooted binary tree, that has a property that each node is either a leaf or has exactly two children. Each leaf has a number associated with it.

On his/her turn a player can choose any two leafs that share their immediate parent, remove them, and associate either of their values with their parent, that now became a leaf (the player decides which of the two values to associate). The game ends when only one node (the one that was the root of the tree) is left.

Pieguy goes first, and his goal is to maximize the value that will be associated with the root when the game ends. Piegirl wants to minimize that value. Assuming that both players are playing optimally, what number will be associated with the root when the game ends?

Input

First line contains a single integer t (1 ≤ t ≤ 100) — number of test cases. Then t test cases follow. Each test case begins with an empty line, followed by a line with a single integer n (1 ≤ n ≤ 250), followed by n lines describing n nodes of the tree. Each of those n lines either contains a non-negative number ai, indicating a leaf node with value ai (0 ≤ ai ≤ 1000) associated with it, or  - 1 followed by integers l and r, indicating a non-leaf node with children l and r (0 ≤ l, r ≤ n - 1). Nodes are numbered from 0 to n - 1. The root is always node 0.

Output

For each test case print one line with one integer on it — the number that will be associated with the root when the game ends.

Examples

Input

4

3
-1 1 2
10
5

5
-1 1 2
-1 3 4
10
5
20

7
-1 1 2
-1 3 4
-1 5 6
1
2
3
4

11
-1 1 2
-1 3 4
-1 5 6
-1 7 8
15
7
-1 9 10
7
8
9
11


Output

10
10
4
8

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using std::cin;
using std::cout;
const int N = 254;
int n;
int p[N], lc[N], rc[N];
int cnt, o[N], id[N], size[N], w[N];
bool f[N][2][3];
inline void up(int &x, const int y) { x < y ? x = y : 0; }
inline void down(int &x, const int y) { x > y ? x = y : 0; }
inline int min(const int x, const int y) { return x < y ? x : y; }
inline int max(const int x, const int y) { return x < y ? y : x; }
int dfs(int x) {
  o[cnt] = x, id[x] = cnt++;
  if (~w[x]) return size[x] = 0;
  size[x] = 1 + dfs(lc[x]);
  return size[x] += dfs(rc[x]);
}
bool check(int th) {
  int i, x, l, r;
  for (i = n - 1; i >= 0; --i) {
    x = o[i];
    if (~w[x]) {
      memset(f[x], w[x] >= th, 6);
      continue;
    }
    l = lc[x], r = rc[x];
    if (~w[l] && ~w[r]) {
      f[x][0][0] = f[x][0][1] = f[x][1][2] = min(w[l], w[r]) >= th,
      f[x][1][0] = f[x][1][1] = f[x][0][2] = max(w[l], w[r]) >= th;
      continue;
    }
    switch ((size[l] & 1) | (size[r] & 1) << 1) {
      case 0:
        f[x][0][0] = f[l][0][0] && f[r][0][0],
        f[x][1][0] = f[l][1][0] || f[r][1][0],
        f[x][0][1] = f[x][1][0] &&
                     (!size[l] || f[l][0][1 + !size[r]] || f[r][1][0]) &&
                     (!size[r] || f[l][1][0] || f[r][0][1 + !size[l]]),
        f[x][1][1] = f[x][0][0] ||
                     (size[l] && f[l][1][1 + !size[r]] && f[r][0][0]) ||
                     (size[r] && f[l][0][0] && f[r][1][1 + !size[l]]),
        f[x][0][2] = f[x][1][0] && (!size[l] || f[l][0][1] || f[r][1][0]) &&
                     (!size[r] || f[l][1][0] || f[r][0][1]),
        f[x][1][2] = f[x][0][0] || (size[l] && f[l][1][1] && f[r][0][0]) ||
                     (size[r] && f[l][0][0] && f[r][1][1]);
        break;
      case 1:
        f[x][0][0] = (f[l][0][0] || f[r][1][0]) && (f[l][1][1] || f[r][0][1]),
        f[x][1][0] = (f[l][1][0] && f[r][0][0]) || (f[l][0][1] && f[r][1][1]),
        f[x][0][1] = f[x][1][0] && f[l][0][1 + !size[r]] && f[r][0][0],
        f[x][1][1] = f[x][0][0] || f[l][1][1 + !size[r]] || f[r][1][0],
        f[x][0][2] = f[x][1][0] && f[l][0][1] && f[r][0][0],
        f[x][1][2] = f[x][0][0] || f[l][1][1] || f[r][1][0];
        break;
      case 2:
        f[x][0][0] = (f[l][1][0] || f[r][0][0]) && (f[l][0][1] || f[r][1][1]),
        f[x][1][0] = (f[l][0][0] && f[r][1][0]) || (f[l][1][1] && f[r][0][1]),
        f[x][0][1] = f[x][1][0] && f[l][0][0] && f[r][0][1 + !size[l]],
        f[x][1][1] = f[x][0][0] || f[l][1][0] || f[r][1][1 + !size[l]],
        f[x][0][2] = f[x][1][0] && f[l][0][0] && f[r][0][1],
        f[x][1][2] = f[x][0][0] || f[l][1][0] || f[r][1][1];
        break;
      case 3:
        f[x][0][0] = f[l][0][1] && f[r][0][1],
        f[x][1][0] = f[l][1][1] || f[r][1][1],
        f[x][0][1] = f[x][1][0] &&
                     (f[l][1][1 + (size[r] == 1)] || f[r][0][0]) &&
                     (f[l][0][0] || f[r][1][1 + (size[l] == 1)]);
        f[x][1][1] = f[x][0][0] ||
                     (f[l][0][1 + (size[r] == 1)] && f[r][1][0]) ||
                     (f[l][1][0] && f[r][0][1 + (size[l] == 1)]);
        f[x][0][2] = f[x][1][0] && (f[l][1][1] || f[r][0][0]) &&
                     (f[l][0][0] || f[r][1][1]);
        f[x][1][2] = f[x][0][0] || (f[l][0][1] && f[r][1][0]) ||
                     (f[l][1][0] && f[r][0][1]);
        break;
    }
  }
  return f[0][1][0];
}
void work() {
  int i, L = INT_MAX, R = INT_MIN, M;
  cin >> n, cnt = 0;
  for (i = 0; i < n; ++i)
    if (cin >> w[i], !~w[i])
      cin >> lc[i] >> rc[i];
    else
      lc[i] = rc[i] = -1, down(L, w[i]), up(R, w[i]);
  dfs(0);
  for (; L < R; check(M) ? L = M : R = M - 1) M = (L + R + 1) / 2;
  cout << L << '\n';
}
int main() {
  int T;
  std::ios::sync_with_stdio(false), cin.tie(NULL);
  for (cin >> T; T; --T) work();
  return 0;
}