#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
using LL = long long;
int main(){
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(0);
int T;
cin >> T;
while(T--){
int n;
cin >> n;
vector<int> a(1 << (n + 1));
for(int i = 2; i < (1 << (n + 1)); i++){
cin >> a[i];
a[i] += a[i / 2];
}
vector<bool> del(1 << (n + 1));
vector<int> cnt(n + 1);
auto check = [&](){
int s = 0;
for(int i = n; i >= 1; i--){
s += cnt[i];
if (s > n - i + 1) return false;
}
return true;
};
int ans = 1e9;
auto dfs = [&](auto &&dfs, int dep) -> void {
if (!check()){
return;
}
int mx = 0;
auto dfs2 = [&](auto &&dfs2, int u) -> vector<int> {
if (del[u]) return {};
if (__lg(u) == n) return {a[u]};
auto v1 = dfs2(dfs2, 2 * u);
auto v2 = dfs2(dfs2, 2 * u + 1);
bool ok = false;
for(int i = 0, j = 0; i < v1.size() and j < v2.size();){
if (v1[i] == v2[j]){
ok = true;
break;
}
if (v1[i] < v2[j]) i++;
else j++;
}
if (ok) mx = max(mx, u);
vector<int> v(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v.begin());
return v;
};
dfs2(dfs2, 1);
if (mx == 0){
ans = min(ans, dep);
return;
}
for (auto t : {mx * 2, mx * 2 + 1}){
del[t] = true;
cnt[__lg(t)] += 1;
dfs(dfs, dep + 1);
cnt[__lg(t)] -= 1;
del[t] = false;
}
};
dfs(dfs, 0);
if (ans > n) cout << -1 << '\n';
else cout << ans << '\n';
}
}