#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using vi = vector<int>;
#define pb push_back
#define eb emplace_back
#define f  first
#define s  second

const int MAXN = 2e5 + 5;
int n, K, a[MAXN], st[MAXN][18];

int gcd(int a, int b) {
	while (b > 0) {
		a %= b;
		swap(a, b);
	}
	return a;
}

bool valid(int k) {
	set<int> s;
	for (int i = 0; i < n; ++i) {
		int x = i;
		int g = st[i][0];
		for (int mask = K; mask >= 0; --mask) {
			if ((k+1)&(1<<mask)) {
				g = gcd(g, st[x][mask]);
				x = (x + (1<<mask)) % n;
			}
		}
		// cout << g << " ";
		s.insert(g);
		if (s.size() > 1) return true;
	}
	// cout << '\n';
	assert(s.size() == 1);
	return false;
}

void solve() {
	cin >> n;
	for (int i = 0; i < n; ++i) cin >> st[i][0];
	K = log(n)/log(2);
	for (int j = 1; j <= K; ++j) {
		for (int i = 0; i < n; ++i) {
			st[i][j] = gcd(st[i][j-1], st[(i+(1<<(j-1)))%n][j-1]);
		}
	}
	// for (int i = 0; i < n; ++i) {
	// 	cout << i << " --> ";
	// 	valid(i);
	// }
	int ans = -1;
	for (int x = n/2; x > 0; x /= 2) {
		while (ans + x < n-1 && valid(ans + x)) {
			ans += x;
		}
	}
	++ans;
	if (ans >= n) {
		for (int i = 0; i < n; ++i) cout << valid(i);
		cout << '\n';
	}
	else cout << ans << '\n';
	// cout << "DONE\n";
}

int main() {
	cin.tie(NULL);
	ios_base::sync_with_stdio(false);

	int T; cin >> T;
	while (T--)
		solve();

	return 0;
}
