#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;
}
