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 MAX_N = int(5e2) + 10;
int n, _;
int a[MAX_N], l[MAX_N], r[MAX_N], s[MAX_N];
int relax(int a, int b, int o) {
  if (o) return min(a, b);
  return max(a, b);
}
void init(int x) {
  if (a[x] == -1) {
    init(l[x]);
    init(r[x]);
    if (s[r[x]] & 1) swap(l[x], r[x]);
    s[x] = s[l[x]] + s[r[x]] + 1;
  } else
    s[x] = 0;
}
int dp[MAX_N][2][3];
int am[MAX_N][2][3];
int solve(int x, int o, int p) {
  if (a[x] != -1) return a[x];
  if (s[x] == 1) return relax(a[l[x]], a[r[x]], o ^ (p == 2));
  int& res = dp[x][o][p];
  if (am[x][o][p] == _) return res;
  am[x][o][p] = _;
  if (p) {
    if (s[x] & 1) {
      res = solve(x, !o, 0);
      if (s[l[x]] & 1) {
        int tmp;
        tmp = relax(solve(l[x], o, 0),
                    solve(r[x], !o, s[l[x]] == 1 && p == 1 ? 2 : 1), !o);
        res = relax(res, tmp, o);
        tmp = relax(solve(r[x], o, 0),
                    solve(l[x], !o, s[r[x]] == 1 && p == 1 ? 2 : 1), !o);
        res = relax(res, tmp, o);
      } else {
        if (s[r[x]]) {
          int tmp = relax(solve(l[x], !o, 0),
                          solve(r[x], o, s[l[x]] == 0 && p == 1 ? 2 : 1), !o);
          res = relax(res, tmp, o);
        }
        if (s[l[x]]) {
          int tmp = relax(solve(r[x], !o, 0),
                          solve(l[x], o, s[r[x]] == 0 && p == 1 ? 2 : 1), !o);
          res = relax(res, tmp, o);
        }
      }
    } else {
      int tmp = solve(l[x], o, s[r[x]] == 0 && p == 1 ? 2 : 1);
      res = relax(solve(r[x], o, 0), tmp, o);
    }
  } else {
    if (s[x] & 1) {
      if (s[l[x]] & 1)
        res = relax(solve(l[x], o, 1), solve(r[x], o, 1), o);
      else
        res = relax(solve(l[x], o, 0), solve(r[x], o, 0), o);
    } else {
      int tmp1 = relax(solve(l[x], o, 0), solve(r[x], !o, 0), !o);
      int tmp2 = relax(solve(r[x], o, 1), solve(l[x], !o, 1), !o);
      res = relax(tmp1, tmp2, o);
    }
  }
  return res;
}
int main() {
  for (scanf("%d", &_); _; --_) {
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
      scanf("%d", a + i);
      if (a[i] == -1) scanf("%d%d", l + i, r + i);
    }
    init(0);
    printf("%d\n", solve(0, 0, 0));
  }
  return 0;
}