#include <bits/stdc++.h>
#define pb push_back
#define ms(_data, v) memset(_data, v, sizeof(_data))
#define SZ(a) int((a).size())
using namespace std;
typedef long long ll;
const ll inf = 0x3f3f3f3f;
const int N = 4e5 + 5;
int T, n;
int a[N];
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
struct node
{
    int l, r;
    int val;
} t[N << 2];
void pushup(int x)
{
    t[x].val = gcd(t[x << 1].val, t[x << 1 | 1].val);
}
void build(int l, int r, int x = 1)
{
    t[x] = {l, r, a[l]};
    if (l == r)
        return;
    int mid = (l + r) >> 1;
    build(l, mid, x << 1), build(mid + 1, r, x << 1 | 1);
    pushup(x);
}
int ask(int l, int r, int x = 1)
{
    if (l <= t[x].l && r >= t[x].r)
    {
        return t[x].val;
    }
    int mid = (t[x].l + t[x].r) >> 1;
    int now_gcd = 0;
    if (l <= mid)
        now_gcd = ask(l, r, x << 1);
    if (r > mid)
        now_gcd = gcd(now_gcd, ask(l, r, x << 1 | 1));
    return now_gcd;
}
int main()
{
    std::ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> T;
    while (T--)
    {
        cin >> n; // 每进行一轮操作所得的公约数就是往右边更进一位的公共公约数
        int all_gcd = 0;
        for (int i = 1; i <= n; ++i)
        {
            cin >> a[i];
            all_gcd = gcd(all_gcd, a[i]);
        }
        for (int i = 1; i <= n; ++i)
        {
            a[i] /= all_gcd; // 先除以共同的公约数，现在转化为求共公约数为1的最大长度
            a[n + i] = a[i];
        }
        n <<= 1;
        build(1, n);
        int ans = 0;
        for (int i = 1; i <= n / 2; ++i)
        { // 枚举左端起点
            if (a[i] == 1)
                continue;
            int le = i + 1, ri = n; // 二分右端点取值区间，因为随着区间长度的增长，all_gcd下降
            while (le < ri)
            {
                int mid = (le + ri) >> 1;
                int now_gcd = ask(i, mid);
                if (now_gcd == 1)
                    ri = mid;
                else
                    le = mid + 1;
            }
            ans = max(ans, ri - i);
        }
        cout << ans << endl;
    }
    return 0;
}