#include <iostream>
#include <vector>
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define saleh \
ios_base::sync_with_stdio(false); \
cin.tie(nullptr);
#define ll long long
vector < int > a;
int cnt_swaps(int l, int r, int h){
if(l >= r) return 0;
int m = (l + r) / 2;
// left part -> [l, m]
// right part -> [m + 1, r]
int min_left = INT_MAX, min_right = INT_MAX;
for(int i = l; i <= m; i++)
min_left = min(min_left, a[i]);
for(int i = m + 1; i <= r; i++)
min_right = min(min_right, a[i]);
int swaps = min_left > min_right;
if(abs(min_left - min_right) > (1 << h)) return -1;
int left_swaps = cnt_swaps(l, m, h - 1);
int right_swaps = cnt_swaps(m + 1, r, h - 1);
if(left_swaps == -1 || right_swaps == -1) return -1;
return swaps + left_swaps + right_swaps;
}
int log2(int n){
int res = 0;
while(n > 0)
n >>= 1, res++;
return res - 1;
}
void Solve(){
int n;
cin >> n;
a = vector < int > (n);
for(auto& x : a)
cin >> x;
cout << cnt_swaps(0, n - 1, log2(n) - 1) << '\n';
}
int main(){
int test_cases = 1;
cin >> test_cases;
for(int tc = 1; tc <= test_cases; tc++){
Solve();
}
return 0;
}
CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKI2RlZmluZSBzYWxlaCAgICAgICAgICAgICAgICAgICAgICAgICBcCiAgICBpb3NfYmFzZTo6c3luY193aXRoX3N0ZGlvKGZhbHNlKTsgXAogICAgY2luLnRpZShudWxscHRyKTsKI2RlZmluZSBsbCBsb25nIGxvbmcKCgp2ZWN0b3IgPCBpbnQgPiBhOwoKaW50IGNudF9zd2FwcyhpbnQgbCwgaW50IHIsIGludCBoKXsKICAgIGlmKGwgPj0gcikgcmV0dXJuIDA7CiAgICBpbnQgbSA9IChsICsgcikgLyAyOwogICAgLy8gbGVmdCBwYXJ0IC0+IFtsLCBtXQogICAgLy8gcmlnaHQgcGFydCAtPiBbbSArIDEsIHJdCiAgICBpbnQgbWluX2xlZnQgPSBJTlRfTUFYLCBtaW5fcmlnaHQgPSBJTlRfTUFYOwogICAgZm9yKGludCBpID0gbDsgaSA8PSBtOyBpKyspCiAgICAgICAgbWluX2xlZnQgPSBtaW4obWluX2xlZnQsIGFbaV0pOwogICAgZm9yKGludCBpID0gbSArIDE7IGkgPD0gcjsgaSsrKQogICAgICAgIG1pbl9yaWdodCA9IG1pbihtaW5fcmlnaHQsIGFbaV0pOwogICAgaW50IHN3YXBzID0gbWluX2xlZnQgPiBtaW5fcmlnaHQ7CiAgICBpZihhYnMobWluX2xlZnQgLSBtaW5fcmlnaHQpID4gKDEgPDwgaCkpIHJldHVybiAtMTsKICAgIGludCBsZWZ0X3N3YXBzID0gY250X3N3YXBzKGwsIG0sIGggLSAxKTsKICAgIGludCByaWdodF9zd2FwcyA9IGNudF9zd2FwcyhtICsgMSwgciwgaCAtIDEpOwogICAgaWYobGVmdF9zd2FwcyA9PSAtMSB8fCByaWdodF9zd2FwcyA9PSAtMSkgcmV0dXJuIC0xOwogICAgcmV0dXJuIHN3YXBzICsgbGVmdF9zd2FwcyArIHJpZ2h0X3N3YXBzOwp9CgppbnQgbG9nMihpbnQgbil7CiAgICBpbnQgcmVzID0gMDsKICAgIHdoaWxlKG4gPiAwKQogICAgICAgIG4gPj49IDEsIHJlcysrOwogICAgcmV0dXJuIHJlcyAtIDE7Cn0KCnZvaWQgU29sdmUoKXsKICAgIGludCBuOwogICAgY2luID4+IG47CiAgICBhID0gdmVjdG9yIDwgaW50ID4gKG4pOwogICAgZm9yKGF1dG8mIHggOiBhKQogICAgICAgIGNpbiA+PiB4OwogICAgY291dCA8PCBjbnRfc3dhcHMoMCwgbiAtIDEsIGxvZzIobikgLSAxKSA8PCAnXG4nOwp9CgppbnQgbWFpbigpewogICAgaW50IHRlc3RfY2FzZXMgPSAxOwogICAgY2luID4+IHRlc3RfY2FzZXM7CiAgICBmb3IoaW50IHRjID0gMTsgdGMgPD0gdGVzdF9jYXNlczsgdGMrKyl7CiAgICAgICAgU29sdmUoKTsKICAgIH0KICAgIHJldHVybiAwOwp9