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 maxn = 255;
inline int read() {
  int x = 0, f = 1;
  char ch = 0;
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-') f = -1;
  for (; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
  return x * f;
}
struct node {
  int l, r, val;
} tr[maxn];
int dp[maxn][3][4], T, n, cz[maxn];
void dfs(int u) {
  if (tr[u].val != -1) return;
  dfs(tr[u].l);
  cz[u] += cz[tr[u].l];
  dfs(tr[u].r);
  cz[u] += cz[tr[u].r];
  cz[u]++;
  if (cz[tr[u].r] & 1) swap(tr[u].l, tr[u].r);
}
inline int cmp(int a, int b, bool c) {
  if (a == 0 || b == 0) return a + b;
  return (a > b) ^ c ? a : b;
}
inline int solve(int u, int player, int skip) {
  if (tr[u].val > 0) return tr[u].val;
  if (dp[u][player][skip]) return dp[u][player][skip];
  if (cz[u] == 1)
    return cmp(tr[tr[u].l].val, tr[tr[u].r].val, player ^ (skip == 2));
  int res = 0;
  if (skip) {
    if (cz[u] & 1) {
      if (skip == 1) res = solve(u, !player, 0);
      if (cz[tr[u].l] & 1) {
        res = cmp(
            res,
            cmp(solve(tr[u].l, player, 0),
                solve(tr[u].r, !player, cz[tr[u].l] == 1 && skip == 1 ? 2 : 1),
                !player),
            player);
        res = cmp(
            res,
            cmp(solve(tr[u].r, player, 0),
                solve(tr[u].l, !player, cz[tr[u].r] == 1 && skip == 1 ? 2 : 1),
                !player),
            player);
      } else {
        if (cz[tr[u].r])
          res =
              cmp(res,
                  cmp(solve(tr[u].l, !player, 0),
                      solve(tr[u].r, player, !cz[tr[u].l] && skip == 1 ? 2 : 1),
                      !player),
                  player);
        if (cz[tr[u].l])
          res =
              cmp(res,
                  cmp(solve(tr[u].r, !player, 0),
                      solve(tr[u].l, player, !cz[tr[u].r] && skip == 1 ? 2 : 1),
                      !player),
                  player);
      }
    } else
      res = cmp(solve(tr[u].r, player, 0),
                solve(tr[u].l, player, !cz[tr[u].r] && skip == 1 ? 2 : 1),
                player);
  } else {
    if (cz[u] & 1) {
      if (cz[tr[u].l] & 1)
        res = cmp(solve(tr[u].l, player, 1), solve(tr[u].r, player, 1), player);
      else
        res = cmp(solve(tr[u].l, player, 0), solve(tr[u].r, player, 0), player);
    } else {
      int p =
          cmp(solve(tr[u].l, player, 0), solve(tr[u].r, !player, 0), !player);
      int q =
          cmp(solve(tr[u].l, !player, 1), solve(tr[u].r, player, 1), !player);
      res = cmp(p, q, player);
    }
  }
  return dp[u][player][skip] = res;
}
int main() {
  T = read();
  while (T--) {
    n = read();
    for (int i = 0; i <= n; ++i) tr[i].val = 0, tr[i].l = 0, tr[i].r = 0;
    memset(cz, 0, sizeof(cz));
    memset(dp, 0, sizeof(dp));
    for (int i = 0; i < n; ++i) {
      int opt = read(), l, r;
      if (opt == -1)
        tr[i].val = -1, tr[i].l = read(), tr[i].r = read();
      else
        tr[i].val = opt + 1;
    }
    dfs(0);
    printf("%d\n", solve(0, 0, 0) - 1);
  }
}