#define _USE_MATH_DEFINES #include using namespace std; #define FOR(i,m,n) for(int i=(m);i<(n);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) (v).begin(),(v).end() using ll = long long; constexpr int INF = 0x3f3f3f3f; constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL; constexpr double EPS = 1e-8; constexpr int MOD = 1000000007; // constexpr int MOD = 998244353; constexpr int DY4[]{1, 0, -1, 0}, DX4[]{0, -1, 0, 1}; constexpr int DY8[]{1, 1, 0, -1, -1, -1, 0, 1}; constexpr int DX8[]{0, -1, -1, -1, 0, 1, 1, 1}; template inline bool chmax(T& a, U b) { return a < b ? (a = b, true) : false; } template inline bool chmin(T& a, U b) { return a > b ? (a = b, true) : false; } struct IOSetup { IOSetup() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << fixed << setprecision(20); } } iosetup; void solve() { int n; cin >> n; vector a(n); REP(i, n) cin >> a[i]; map>, pair> dp; auto f = [&](auto&& f, const int light, const vector& a, bool taro) -> int { if (a.empty()) return __builtin_popcount(light); if (dp.count({light, a})) return dp[{light, a}].first; int score = (taro ? -INF : INF), choice = -1; const int n = a.size(); REP(i, n) { if (i > 0 && a[i] == a[i - 1]) continue; vector b = a; b.erase(b.begin() + i); if (taro) { if (chmax(score, f(f, light | ((1 << a[i]) - 1), b, !taro))) choice = a[i]; } else { if (chmin(score, f(f, light & (~0 ^ ((1 << a[i]) - 1)), b, !taro))) choice = a[i]; } } dp[{light, a}] = {score, choice}; return score; }; cout << f(f, 0, a, true) << '\n'; // for (const auto [key, val] : dp) { // const auto [light, a] = key; // cout << bitset<10>(light) << ' '; // REP(i, a.size()) cout << a[i] << ",:"[i + 1 == a.size()]; // cout << ' ' << val.first << ' ' << val.second << '\n'; // } // for (int light = 0, taro = 1; !a.empty(); taro ^= 1) { // const int choice = dp[{light, a}].second; // cout << choice << " \n"[a.size() == 1]; // a.erase(lower_bound(ALL(a), choice)); // if (taro) { // light |= (1 << choice) - 1; // } else { // light &= ~0 ^ ((1 << choice) - 1); // } // } } int main() { int t; cin >> t; while (t--) solve(); return 0; }