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 namespace std;
const int M = 305;
int read() {
  int x = 0, y = 1;
  char ch = getchar();
  while (ch < '0' || ch > '9') y = (ch == '-') ? -1 : 1, ch = getchar();
  while (ch >= '0' && ch <= '9')
    x = (x << 3) + (x << 1) + ch - '0', ch = getchar();
  return x * y;
}
int n, a[M], ls[M], rs[M], sz[M], dp[M][3][3];
bool vis[M][3][3];
void dfs(int u) {
  if (a[u] != -1) return sz[u] = 0, void();
  dfs(ls[u]), dfs(rs[u]), sz[u] = sz[ls[u]] + sz[rs[u]] + 1;
  if (sz[rs[u]] & 1) swap(ls[u], rs[u]);
}
int get(int a, int b, int typ) { return typ ? min(a, b) : max(a, b); }
int dfs(int u, int typ, int fl) {
  if (a[u] != -1) return a[u];
  if (sz[u] == 1) return get(a[ls[u]], a[rs[u]], typ ^ (fl == 2));
  if (vis[u][typ][fl]) return dp[u][typ][fl];
  vis[u][typ][fl] = 1;
  int res;
  if (!fl) {
    if (sz[u] & 1) {
      int sth = sz[ls[u]] & 1;
      res = get(dfs(ls[u], typ, sth), dfs(rs[u], typ, sth), typ);
    } else
      res = get(get(dfs(ls[u], typ, 0), dfs(rs[u], !typ, 0), !typ),
                get(dfs(rs[u], typ, 1), dfs(ls[u], !typ, 1), !typ), typ);
  } else {
    if (sz[u] & 1) {
      res = dfs(u, !typ, 0);
      int sth = sz[ls[u]] & 1;
      if (sth || sz[rs[u]])
        res =
            get(res,
                get(dfs(ls[u], 1 ^ typ ^ sth, 0),
                    dfs(rs[u], typ ^ sth, sz[ls[u]] == sth && fl == 1 ? 2 : 1),
                    !typ),
                typ);
      if (sth || sz[ls[u]])
        res =
            get(res,
                get(dfs(rs[u], 1 ^ typ ^ sth, 0),
                    dfs(ls[u], typ ^ sth, sz[rs[u]] == sth && fl == 1 ? 2 : 1),
                    !typ),
                typ);
    } else
      res = get(dfs(rs[u], typ, 0),
                dfs(ls[u], typ, !sz[rs[u]] && fl == 1 ? 2 : 1), typ);
  }
  return dp[u][typ][fl] = res;
}
void solve() {
  memset(vis, 0, sizeof(vis));
  n = read();
  for (int i = 1; i <= n; i++) {
    a[i] = read();
    if (a[i] == -1) ls[i] = read() + 1, rs[i] = read() + 1;
  }
  dfs(1);
  printf("%d\n", dfs(1, 0, 0));
}
signed main() {
  int T = read();
  while (T--) solve();
}