#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T; cin >> T;
while (T--) {
int n; cin >> n;
string s; cin >> s;
bool all1 = true;
for (char c : s) if (c == '0') { all1 = false; break; }
if (all1) { cout << 0 << '\n'; continue; }
// collect divisors of n
vector<int> divs;
for (int d = 1; d * d <= n; ++d) {
if (n % d == 0) {
divs.push_back(d);
if (d * d != n) divs.push_back(n / d);
}
}
sort(divs.begin(), divs.end());
long long answer = LLONG_MAX;
for (int g : divs) {
int m = n / g; // length of each cycle/class
int worst_run = 0; // maximum zero-run among all classes for this g
for (int r = 0; r < g; ++r) {
// build sequence of bits in this residue class
// We don't need to allocate: we can iterate twice length m to handle cyclic runs.
int cnt = 0;
int max_cnt = 0;
for (int t = 0; t < 2 * m; ++t) {
int idx = r + (t % m) * g; // position in original string
if (s[idx] == '0') {
++cnt;
if (cnt > m) cnt = m; // cap at m
} else {
if (cnt > max_cnt) max_cnt = cnt;
cnt = 0;
}
}
if (cnt > max_cnt) max_cnt = cnt; // final check
max_cnt = min(max_cnt, m);
worst_run = max(worst_run, max_cnt);
if (worst_run == m) break; // can't get bigger
}
long long cost = 1LL * g * worst_run;
answer = min(answer, cost);
}
cout << answer << '\n';
}
return 0;
}