#include <bits/stdc++.h>
using namespace std;
/*
We implement exactly the plan described above.
For each test case:
1) Read n, read array a[1..n].
2) Compute g = gcd(a[1],a[2],...,a[n]).
3) Count how many a[i] already equal g. If cnt > 0, answer = n - cnt.
4) Otherwise, run a small DP to find the minimum subset-size k whose gcd is exactly g.
Then answer = (k - 1) + (n - 1) = n + k - 2.
*/
static int gcd_fast(int x, int y) {
// Use builtin if available; otherwise do slow version:
return std::__gcd(x, y);
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
// 1) Compute overall gcd g:
int g = a[0];
for (int i = 1; i < n; i++) {
g = gcd_fast(g, a[i]);
}
// 2) Count how many are already == g:
int cnt_equal_g = 0;
for (int i = 0; i < n; i++) {
if (a[i] == g) cnt_equal_g++;
}
if (cnt_equal_g > 0) {
// We already have cnt_equal_g copies of g,
// so each of the other (n - cnt_equal_g) must be converted with exactly 1 op each.
cout << (n - cnt_equal_g) << "\n";
continue;
}
// 3) Otherwise, we must “build” our first g by combining some subset.
// Use DP: dp[d] = minimal subset-size whose gcd = d.
// We'll store dp in an unordered_map<int,int> for all (d -> min size).
// Start with dp empty. Process each a[i] in turn.
unordered_map<int,int> dp, newdp;
dp.reserve(n * 4);
newdp.reserve(n * 4);
const int INF = 1e9;
for (int i = 0; i < n; i++) {
newdp = dp;
// (copy all old pairs (d -> dp[d]) into newdp)
// 3a) The singleton subset { a[i] } has gcd = a[i], size = 1.
if (!newdp.count(a[i])) {
newdp[a[i]] = 1;
} else {
newdp[a[i]] = min(newdp[a[i]], 1);
}
// 3b) For each existing gcd-value `old_g` in dp,
// form d' = gcd(old_g, a[i]) by adding a[i].
for (auto &kv : dp) {
int old_g = kv.first;
int old_size = kv.second;
int dprime = gcd_fast(old_g, a[i]);
int new_size = old_size + 1;
if (!newdp.count(dprime) || newdp[dprime] > new_size) {
newdp[dprime] = new_size;
}
}
dp.swap(newdp);
}
// Now dp[g] is the size k of the smallest subset whose gcd = g.
int k = dp[g];
// In order to get our first g, we need (k - 1) gcd-operations on that subset.
// After that, exactly one position = g, and the other (n - 1) are != g,
// each of which takes 1 more operation to convert ⇒ total = (k-1) + (n-1).
int answer = (k - 1) + (n - 1);
cout << answer << "\n";
}
return 0;
}