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