#pragma GCC optimize("Ofast")
#pragma GCC target("popcnt")
#include "bits/stdc++.h"
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i < (n); ++i)
#define rep1n(i, n) for (int i = 1; i <= (n); ++i)
#define repr(i, n) for (int i = (n) - 1; i >= 0; --i)
#define pb push_back
#define eb emplace_back
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define each(x, a) for (auto &x : a)
#define ar array
#define vec vector
#define range(i, n) rep(i, n)
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using str = string;
using pi = pair<int, int>;
using pl = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vpi = vector<pair<int, int>>;
using vvi = vector<vi>;
int Bit(int mask, int b) { return (mask >> b) & 1; }
template<class T>
bool ckmin(T &a, const T &b) {
if (b < a) {
a = b;
return true;
}
return false;
}
template<class T>
bool ckmax(T &a, const T &b) {
if (b > a) {
a = b;
return true;
}
return false;
}
// [l, r)
template<typename T, typename F>
T FindFirstTrue(T l, T r, const F &predicat) {
--l;
while (r - l > 1) {
T mid = l + (r - l) / 2;
if (predicat(mid)) {
r = mid;
} else {
l = mid;
}
}
return r;
}
template<typename T, typename F>
T FindLastFalse(T l, T r, const F &predicat) {
return FindFirstTrue(l, r, predicat) - 1;
}
const int INFi = 2e9;
const ll INF = 2e18;
using Bitset = bitset<1005>;
void solve() {
int n; cin >> n;
vl a(1 << (n + 1));
for(int i = 2; i < a.size(); ++i) {
cin >> a[i];
// cerr << "READ" << endl;
a[i] += a[i / 2];
}
vector<ll> xx;
for(int i = (1 << n); i < a.size(); ++i) {
xx.push_back(a[i]);
}
sort(all(xx));
vector<vector<Bitset>> b(1 << n);
for(int i = (1 << n); i < a.size(); ++i) {
Bitset cur = 0;
int x = lower_bound(all(xx), a[i]) - xx.begin();
cur[x] = 1;
b[i - (1 << n)].push_back(cur);
}
int use = n;
for(int lvl = n; lvl >= 1; --lvl) {
vector<vector<Bitset>> b2(1 << (lvl - 1));
for(int i = (1 << (lvl - 1)); i < (1 << lvl); ++i) {
int x = i * 2;
int y = i * 2 + 1;
x -= (1 << lvl);
y -= (1 << lvl);
int to = i - (1 << (lvl - 1));
vector<Bitset> zero, one;
for(auto &c1 : b[x]) {
for(auto &c2 : b[y]) {
if ((c1 & c2).count()) {
continue;
}
zero.push_back(c1 | c2);
}
}
if (!zero.empty()) {
b2[to] = zero;
} else {
if (use < lvl) {
cout << "-1\n";
return;
}
use--;
b2[to] = b[x];
for(auto &c : b[y]) b2[to].push_back(c);
}
}
swap(b, b2);
}
cout << n - use << '\n';
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout << setprecision(12) << fixed;
int t = 1;
cin >> t;
rep(i, t) {
solve();
}
return 0;
}