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>
int read() {
  int x = 0, f = 1;
  char ch = getchar();
  while ((ch < '0') || (ch > '9')) {
    if (ch == '-') {
      f = -f;
    }
    ch = getchar();
  }
  while ((ch >= '0') && (ch <= '9')) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const int maxn = 200000;
int n, son[2][maxn + 10], size[maxn * 2 + 10], f[maxn + 10][2][3],
    val[maxn + 10];
int getsize(int u) {
  return size[u] =
             ((val[u] != -1) ? 0
                             : (getsize(son[0][u]) + getsize(son[1][u]) + 1));
}
int merge(int a, int b, int k) {
  return (a == -1) ? b : ((b == -1) ? a : (((a > b) ^ k) ? a : b));
}
int search(int u, int p, int s) {
  if (!size[u]) {
    return val[u];
  }
  if (~f[u][p][s]) {
    return f[u][p][s];
  }
  int &res = f[u][p][s];
  if (size[u] == 1) {
    return res = merge(val[son[0][u]], val[son[1][u]], p ^ (s == 2));
  }
  if (s) {
    if (s == 1) {
      res = search(u, !p, 0);
    }
    if (size[u] & 1) {
      if (size[son[0][u]] & 1) {
        res = merge(res,
                    merge(search(son[0][u], p, 0),
                          search(son[1][u], !p,
                                 ((size[son[0][u]] == 1) && (s == 1)) + 1),
                          !p),
                    p);
        res = merge(res,
                    merge(search(son[1][u], p, 0),
                          search(son[0][u], !p,
                                 ((size[son[1][u]] == 1) && (s == 1)) + 1),
                          !p),
                    p);
      } else {
        if (size[son[1][u]]) {
          res = merge(res,
                      merge(search(son[0][u], !p, 0),
                            search(son[1][u], p,
                                   ((size[son[0][u]] == 0) && (s == 1)) + 1),
                            !p),
                      p);
        }
        if (size[son[0][u]]) {
          res = merge(res,
                      merge(search(son[1][u], !p, 0),
                            search(son[0][u], p,
                                   ((size[son[1][u]] == 0) && (s == 1)) + 1),
                            !p),
                      p);
        }
      }
    } else {
      res = merge(
          res,
          merge(search(son[1][u], p, 0),
                search(son[0][u], p, ((size[son[1][u]] == 0) && (s == 1)) + 1),
                p),
          p);
    }
  } else {
    if (size[u] & 1) {
      if (size[son[0][u]] & 1) {
        res = merge(search(son[0][u], p, 1), search(son[1][u], p, 1), p);
      } else {
        res = merge(search(son[0][u], p, 0), search(son[1][u], p, 0), p);
      }
    } else {
      res = merge(merge(search(son[0][u], p, 0), search(son[1][u], !p, 0), !p),
                  merge(search(son[0][u], !p, 1), search(son[1][u], p, 1), !p),
                  p);
    }
  }
  return res;
}
int main() {
  int T = read();
  while (T--) {
    n = read();
    for (int i = 0; i < n; ++i) {
      int fl = read();
      if (fl == -1) {
        son[0][i] = read();
        son[1][i] = read();
      } else {
        son[0][i] = 0;
        son[1][i] = 0;
      }
      val[i] = fl;
    }
    getsize(0);
    memset(f, -1, sizeof f);
    for (int i = 0; i < n; ++i) {
      if (size[son[1][i]] & 1) {
        std::swap(son[0][i], son[1][i]);
      }
    }
    printf("%d\n", search(0, 0, 0));
  }
  return 0;
}