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;
int MysteryTableOfMystery[28][28] = {
    {2, 2, 1, 2, 2, 4,  2, 6, 9, 1,  1,  12, 1,  3,
     3, 3, 9, 4, 4, 12, 4, 9, 6, 10, 12, 6,  27, 3},
    {2, 2, 1, 2, 2, 4,  2, 6, 9, 1,  1,  12, 1,  4,
     4, 4, 9, 4, 4, 12, 4, 9, 6, 12, 12, 6,  12, 4},
    {1, 1, 2, 1, 1, 9, 1, 12, 4,  2, 2, 6,  2, 9,
     9, 9, 4, 9, 9, 6, 9, 4,  12, 6, 6, 12, 6, 9},
    {2, 2, 1, 2, 2, 4,  2, 6, 9, 1,  1,  12, 1,  4,
     4, 4, 9, 4, 4, 12, 4, 9, 6, 12, 12, 6,  15, 4},
    {2, 2,  1, 2, 2, 4,  5,  6,  9, 1,  1,  12, 8,  4,
     4, 16, 9, 4, 4, 12, 16, 17, 6, 20, 20, 21, 20, 4},
    {4,  4,  9, 4,  4,  12, 4,  12, 6,  9, 9, 6,  9, 12,
     12, 12, 6, 12, 12, 6,  12, 6,  12, 6, 6, 12, 6, 12},
    {2,  2,  1,  2,  5,  4,  7,  6,  9,  8,  8,  12, 11, 16,
     16, 19, 17, 16, 16, 20, 19, 22, 21, 25, 25, 24, 25, 16},
    {6,  6,  12, 6,  6,  12, 6,  12, 6,  12, 12, 6,  12, 12,
     12, 12, 6,  12, 12, 6,  12, 6,  12, 6,  6,  12, 6,  12},
    {9, 9, 4,  9, 9, 6,  9, 6,  12, 4,  4,  12, 4,  6,
     6, 6, 12, 6, 6, 12, 6, 12, 6,  12, 12, 6,  12, 6},
    {1, 1,  2, 1, 1, 9, 8,  12, 4,  2,  2,  6,  5,  9,
     9, 17, 4, 9, 9, 6, 17, 16, 12, 21, 21, 20, 21, 9},
    {1, 1,  2, 1, 1, 9, 8,  12, 4,  2,  2,  6,  5,  9,
     9, 17, 4, 9, 9, 6, 17, 16, 12, 21, 21, 20, 23, 13},
    {12, 12, 6,  12, 12, 6,  12, 6,  12, 6,  6,  12, 6,  6,
     6,  6,  12, 6,  6,  12, 6,  12, 6,  12, 12, 6,  12, 6},
    {1,  1,  2,  1,  8,  9,  11, 12, 4,  5,  5,  6,  7,  17,
     17, 22, 16, 17, 17, 21, 22, 19, 20, 24, 24, 25, 24, 17},
    {3,  4,  9, 4,  4,  12, 16, 12, 6,  9,  9,  6,  17, 12,
     12, 20, 6, 12, 12, 6,  20, 21, 12, 21, 21, 20, 21, 10},
    {3,  4,  9, 4,  4,  12, 16, 12, 6,  9,  9,  6,  17, 12,
     12, 20, 6, 12, 12, 6,  20, 21, 12, 21, 21, 20, 23, 14},
    {3,  4,  9,  4,  16, 12, 19, 12, 6,  17, 17, 6,  22, 20,
     20, 25, 21, 20, 20, 21, 25, 24, 20, 24, 24, 25, 24, 18},
    {9, 9,  4,  9, 9, 6,  17, 6,  12, 4,  4,  12, 16, 6,
     6, 21, 12, 6, 6, 12, 21, 20, 6,  20, 20, 21, 20, 6},
    {4,  4,  9, 4,  4,  12, 16, 12, 6,  9,  9,  6,  17, 12,
     12, 20, 6, 12, 12, 6,  20, 21, 12, 21, 21, 20, 21, 12},
    {4,  4,  9, 4,  4,  12, 16, 12, 6,  9,  9,  6,  17, 12,
     12, 20, 6, 12, 12, 6,  20, 21, 12, 21, 21, 20, 23, 15},
    {12, 12, 6,  12, 12, 6,  20, 6,  12, 6,  6,  12, 21, 6,
     6,  21, 12, 6,  6,  12, 21, 20, 6,  20, 20, 21, 20, 6},
    {4,  4,  9,  4,  16, 12, 19, 12, 6,  17, 17, 6,  22, 20,
     20, 25, 21, 20, 20, 21, 25, 24, 20, 24, 24, 25, 24, 20},
    {9,  9,  4,  9,  17, 6,  22, 6,  12, 16, 16, 12, 19, 21,
     21, 24, 20, 21, 21, 20, 24, 25, 21, 25, 25, 24, 25, 21},
    {6,  6,  12, 6,  6,  12, 21, 12, 6,  12, 12, 6,  20, 12,
     12, 20, 6,  12, 12, 6,  20, 21, 12, 21, 21, 20, 21, 12},
    {10, 12, 6,  12, 20, 6,  25, 6,  12, 21, 21, 12, 24, 21,
     21, 24, 20, 21, 21, 20, 24, 25, 21, 25, 25, 24, 25, 21},
    {12, 12, 6,  12, 20, 6,  25, 6,  12, 21, 21, 12, 24, 21,
     21, 24, 20, 21, 21, 20, 24, 25, 21, 25, 25, 24, 25, 21},
    {6,  6,  12, 6,  21, 12, 24, 12, 6,  20, 20, 6,  25, 20,
     20, 25, 21, 20, 20, 21, 25, 24, 20, 24, 24, 25, 24, 20},
    {27, 12, 6,  15, 20, 6,  25, 6,  12, 21, 23, 12, 24, 21,
     23, 24, 20, 21, 23, 20, 24, 25, 21, 25, 25, 24, 25, 23},
    {3,  4,  9, 4,  4,  12, 16, 12, 6,  9,  13, 6,  17, 10,
     14, 18, 6, 12, 15, 6,  20, 21, 12, 21, 21, 20, 23, 14}};
int N;
int v[251], lc[251], rc[251];
int goal;
int doit(int x) {
  if (v[x] != -1) return v[x] >= goal ? 26 : 0;
  return MysteryTableOfMystery[doit(lc[x])][doit(rc[x])];
}
int main() {
  int T;
  for (cin >> T; T--;) {
    cin >> N;
    for (int i = 0; i < N; i++) {
      cin >> v[i];
      if (v[i] == -1) cin >> lc[i] >> rc[i];
    }
    int lo = 0, hi = 1000;
    while (lo < hi) {
      goal = (lo + hi + 1) / 2;
      if (doit(0) >= 8)
        lo = goal;
      else
        hi = goal - 1;
    }
    cout << lo << endl;
  }
}