#include <bits/stdc++.h>
using namespace std;
const int ma = 502;
int S[ma], f[ma][2][3], z[ma], l[ma], r[ma], i, j, k, n, m, T;
char c;
int read() {
  int k = 1, z = 0;
  do c = getchar();
  while ((c < '0' || c > '9') & c != '-');
  if (c == '-') k = -1, c = getchar();
  while (c >= '0' && c <= '9') z *= 10, z += c - '0', c = getchar();
  return k * z;
}
int DFS(int w) {
  if (~z[w]) return S[w] = 0;
  S[w] = 1 + DFS(l[w]) + DFS(r[w]);
  if (S[r[w]] & 1) swap(l[w], r[w]);
  return S[w];
}
int merge(int a, int b, bool f) {
  if (a > b) swap(a, b);
  if (a < 0) return b;
  return f ? a : b;
}
int dfs(int w, int p, int s) {
  if (~z[w]) return z[w];
  int L = l[w], R = r[w];
  if (S[w] == 1) return merge(z[L], z[R], p ^ (s == 2));
  int& d = f[w][p][s];
  if (~d) return d;
  if (s) {
    if (s == 1) d = dfs(w, !p, 0);
    if (S[w] & 1)
      if (S[L] & 1)
        d = merge(
            d, merge(dfs(L, p, 0), dfs(R, !p, S[L] == 1 && s == 1 ? 2 : 1), !p),
            p),
        d = merge(
            d, merge(dfs(R, p, 0), dfs(L, !p, S[R] == 1 && s == 1 ? 2 : 1), !p),
            p);
      else {
        if (S[R])
          d = merge(
              d, merge(dfs(L, !p, 0), dfs(R, p, !S[L] && s == 1 ? 2 : 1), !p),
              p);
        if (S[L])
          d = merge(
              d, merge(dfs(R, !p, 0), dfs(L, p, !S[R] && s == 1 ? 2 : 1), !p),
              p);
      }
    else
      d = merge(d, merge(dfs(R, p, 0), dfs(L, p, !S[R] && s == 1 ? 2 : 1), p),
                p);
  } else {
    if (S[w] & 1)
      if (S[L] & 1)
        d = merge(dfs(L, p, 1), dfs(R, p, 1), p);
      else
        d = merge(dfs(L, p, 0), dfs(R, p, 0), p);
    else
      d = merge(merge(dfs(L, p, 0), dfs(R, !p, 0), !p),
                merge(dfs(L, !p, 1), dfs(R, p, 1), !p), p);
  }
  return d;
}
int main() {
  T = read();
  while (T--) {
    n = read();
    for (i = 1; i <= n; i++)
      if ((z[i] = read()) < 0) l[i] = read() + 1, r[i] = read() + 1;
    memset(f, -1, sizeof f);
    DFS(1), printf("%d\n", dfs(1, 0, 0));
  }
  return 0;
}
