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
#include <bits/stdc++.h>
using namespace std;
const int N = 255;
int n, mid;
int ls[N], rs[N], a[N], sz[N];
int f[N][2][3], vis[N][2][3];
int dfs(int x) {
  if (a[x] != -1) return sz[x] = 0;
  sz[x] = dfs(ls[x]) + dfs(rs[x]) + 1;
  if (sz[rs[x]] & 1) swap(ls[x], rs[x]);
  return sz[x];
}
bool merge(bool x, bool y, bool tp) { return tp ? x & y : x | y; }
bool dp(int n, int p, int s) {
  if (a[n] != -1) return a[n] >= mid;
  if (sz[n] == 1) return merge(a[ls[n]] >= mid, a[rs[n]] >= mid, p ^ (s == 2));
  if (vis[n][p][s]) return f[n][p][s];
  vis[n][p][s] = 1;
  if (s) {
    if (!(sz[ls[n]] & 1)) {
      f[n][p][s] = dp(n, !p, 0);
      if (sz[rs[n]]) {
        int val0 = dp(rs[n], p, 1 + (!sz[ls[n]] && s == 1));
        int val1 = dp(ls[n], !p, 0);
        int val = merge(val0, val1, !p);
        f[n][p][s] = merge(f[n][p][s], val, p);
      }
      if (sz[ls[n]]) {
        int val0 = dp(ls[n], p, 1 + (!sz[rs[n]] && s == 1));
        int val1 = dp(rs[n], !p, 0);
        int val = merge(val0, val1, !p);
        f[n][p][s] = merge(f[n][p][s], val, p);
      }
    } else if (sz[rs[n]] & 1) {
      f[n][p][s] = dp(n, !p, 0);
      {
        int val0 = dp(rs[n], !p, 1 + (sz[ls[n]] == 1 && s == 1));
        int val1 = dp(ls[n], p, 0);
        int val = merge(val0, val1, !p);
        f[n][p][s] = merge(f[n][p][s], val, p);
      }
      {
        int val0 = dp(ls[n], !p, 1 + (sz[rs[n]] == 1 && s == 1));
        int val1 = dp(rs[n], p, 0);
        int val = merge(val0, val1, !p);
        f[n][p][s] = merge(f[n][p][s], val, p);
      }
    } else {
      int val0 = dp(rs[n], p, 0);
      int val1 = dp(ls[n], p, 1 + (!sz[rs[n]] && s == 1));
      f[n][p][s] = merge(val0, val1, p);
    }
  } else {
    if (!(sz[ls[n]] & 1))
      f[n][p][s] = merge(dp(ls[n], p, 0), dp(rs[n], p, 0), p);
    else if (sz[rs[n]] & 1)
      f[n][p][s] = merge(dp(ls[n], p, 1), dp(rs[n], p, 1), p);
    else {
      int val0 = merge(dp(ls[n], p, 0), dp(rs[n], !p, 0), !p);
      int val1 = merge(dp(rs[n], p, 1), dp(ls[n], !p, 1), !p);
      f[n][p][s] = merge(val0, val1, p);
    }
  }
  return f[n][p][s];
}
void solve() {
  scanf("%d", &n);
  for (int i = (int)(0); i <= (int)(n - 1); i++) {
    scanf("%d", &a[i]);
    if (a[i] == -1) scanf("%d%d", &ls[i], &rs[i]);
  }
  dfs(0);
  int l = 0, r = 1001, ans = 0;
  while (l <= r) {
    mid = (l + r) / 2;
    memset(vis, 0, sizeof(vis));
    ;
    if (dp(0, 0, 0))
      ans = mid, l = mid + 1;
    else
      r = mid - 1;
  }
  printf("%d\n", ans);
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) solve();
}
