#include <bits/stdc++.h>
using namespace std;
int t, l[510], r[510], a[510], s[510], n, vi[510][2][3], f[510][2][3];
void d1(int x) {
  if (a[x] == -1) {
    d1(l[x]);
    d1(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 gx(int a, int b, int t) { return t ? min(a, b) : max(a, b); }
int dp(int x, int y, int z) {
  if (a[x] != -1) return a[x];
  if (s[x] == 1) return gx(a[l[x]], a[r[x]], y ^ (z == 2));
  int &rr = f[x][y][z];
  if (vi[x][y][z]) return rr;
  vi[x][y][z] = 1;
  if (z) {
    if (s[x] & 1) {
      rr = dp(x, !y, 0);
      if (s[l[x]] & 1) {
        int tp = gx(dp(l[x], y, 0),
                    dp(r[x], !y, s[l[x]] == 1 && z == 1 ? 2 : 1), !y);
        rr = gx(rr, tp, y);
        tp = gx(dp(r[x], y, 0), dp(l[x], !y, s[r[x]] == 1 && z == 1 ? 2 : 1),
                !y);
        rr = gx(rr, tp, y);
      } else {
        if (s[r[x]])
          rr = gx(
              rr,
              gx(dp(l[x], !y, 0), dp(r[x], y, !s[l[x]] && z == 1 ? 2 : 1), !y),
              y);
        if (s[l[x]])
          rr = gx(
              rr,
              gx(dp(r[x], !y, 0), dp(l[x], y, !s[r[x]] && z == 1 ? 2 : 1), !y),
              y);
      }
    } else
      rr = gx(dp(r[x], y, 0), dp(l[x], y, !s[r[x]] && z == 1 ? 2 : 1), y);
  } else {
    if (s[x] & 1) {
      if (s[l[x]] & 1)
        rr = gx(dp(l[x], y, 1), dp(r[x], y, 1), y);
      else
        rr = gx(dp(l[x], y, 0), dp(r[x], y, 0), y);
    } else
      rr = gx(gx(dp(l[x], y, 0), dp(r[x], !y, 0), !y),
              gx(dp(r[x], y, 1), dp(l[x], !y, 1), !y), y);
  }
  return rr;
}
int main() {
  cin >> t;
  while (t--) {
    memset(vi, 0, sizeof(vi));
    memset(f, 0, sizeof(f));
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> a[i];
      if (a[i] == -1) cin >> l[i] >> r[i];
    }
    d1(0);
    cout << dp(0, 0, 0) << '\n';
  }
}
