﻿#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,abm,mmx,avx,avx2,fma,tune=native"

#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <deque>
#include <functional>
#include <set>
#include <map>
#include <random>
#include <cstdlib>
#include <memory>   
#include <cassert>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <bitset>
#include <time.h>
#include <stack>
#include <queue>
#include <complex>


	using namespace std;
using ll = long long;
using ul = unsigned long long;
using ld = long double;

template<typename T>
void print(T h, string s = " ") { for (auto ijkl : h) cout << ijkl << s; cout << "\n"; }


const long long INF = 1e18;
#define vll vector <ll>
#define ve vector
#define sll set <ll>
#define pll pair <ll, ll>
#define printvpll(h) for (auto ijkl : h) cerr << ijkl.ft << ":" << ijkl.sd << " "; cerr << "\n";
#define scan(h, n) for (int ijkl = 0; ijkl < n; ++ijkl) cin >> h[ijkl]
#define pow2(n) (n)    *    (n)
#define pow3(n) (n) * (n) * (n)
#define all(x) (x).begin(), (x).end()
#define MOD ll(1e9 + 7)
#define MOD2 ll(998244353)
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define ft first
#define sd second
#define look1(x)          cerr << #x << ": " << x << "\n";
#define look2(x, y)       cerr << #x << ": " << x << " | " << #y << ": " << y << "\n";
#define look3(x, y, z)    cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << "\n";
#define look4(a, b, c, d) cerr << #a << ":" << a << " | " << #b << ":" << b << " | " << #c << ":" << c << " | " << #d << ":" << d << "\n";

ll pown(ll n, ll cnt, ll mod = INT64_MAX) {
	return cnt == 0 ? 1 % mod : (cnt % 2 ? pown(n * n % mod, cnt / 2, mod) * n % mod : pown(n * n % mod, cnt / 2, mod) % mod);
}

ll gcd(ll a, ll b) {
	if (b == 0) return a;
	return gcd(b, a % b);
}

string alph = "0123456789abcdefghijklmnopqrstuvwxyz";

vll tree;
vll h;

void build(ll ind, ll l, ll r) {
	if (l >= r) return;
	if (l + 1 == r) {
		tree[ind] = h[l];
		return;
	}
	ll m = (l + r) / 2;
	build(ind * 2 + 1, l, m);
	build(ind * 2 + 2, m, r);
	tree[ind] = gcd(tree[ind * 2 + 1], tree[ind * 2 + 2]);
	return;
}

ll get_gcd(ll ind, ll l, ll r, ll ql, ll qr) {
	if (l >= qr || ql >= r) return 0;
	if (ql <= l && r <= qr) return tree[ind];
	ll m = (l + r) / 2;
	return gcd(get_gcd(ind * 2 + 1, l, m, ql, qr), get_gcd(ind * 2 + 2, m, r, ql, qr));
}

signed main() {
	ios_base::sync_with_stdio(0);
	cout.tie(0);
	cin.tie(0);
	ll t;
	cin >> t;
	while (t--) {
		ll n;
		cin >> n;
		ll to = 0;
		h.assign(n, 0);
		tree.assign(4 * n, 0);
		scan(h, n);
		for (auto it : h) {
			to = gcd(it, to);
		}
		ll ans = 0;
		build(0, 0, n);
		for (int i = 0; i < n; ++i) {
			ll l = -1, r = n - 1;
			while (r - l > 1) {
				ll m = (l + r) / 2;
				ll ql = i, qr = i + m + 1;
				ll g = get_gcd(0, 0, n, ql, min(qr, n));
				if (qr > n) {
					g = gcd(g, get_gcd(0, 0, n, 0, qr - n));
				}
				if (g == to) {
					r = m;
				}
				else {
					l = m;
				}
			}
			ans = max(ans, r);
		}
		cout << ans << "\n";
	}
	return 0;
}