#include<bits/stdc++.h>
using namespace std;
#define ll int
#define pb push_back
#define take vll a(n);for (int i=0;i<n;i++){cin>>a[i];}
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define endl "\n"
#define memory_of_node (struct node *)malloc(sizeof(struct node))
#define vll vector<ll>
#define vpl vector<pair<ll,ll>>
#define mp make_pair
#define s second
#define f first
#define st(v) sort(v.begin(),v.end())
#define Y cout<<"YES"<<endl
#define N cout<<"NO"<<endl
#define SY cout<<"Yes"<<endl
#define SN cout<<"No"<<endl
#define rep(start,upto,increment) for(int j=start;j<upto;j+=increment)
#define sz(a) a.size()
#define mem(a,x) memset(a,x,sizeof(a));
#define mll map<ll,ll>
#define all(a) a.begin(),a.end()
ll vmax(vll &v){
   return (*max_element(all(v)));
}
ll vmin(vll &v){
   return (*min_element(all(v)));
}
ll power_mod_m(ll x,ll y,ll p){
   ll res = 1; 
    x = x % p;  
    if (x == 0) return 0; 
    while (y > 0){
        if (y & 1)
            res = ((res%p)*(x%p)) % p;
        y = y>>1;
        x = ((x%p)*(x%p)) % p;
    }
    return res;
}
ll inverse_mod(ll x,ll mod){
   return (power_mod_m(x,mod-2,mod));
}
const ll M=400005;
ll segment_tree[4*M+10];
ll i=0;
ll a[M];
ll n;
ll gcd(ll a,ll b){
   if (a==0){
      return b;
   }
   return (gcd(b%a,a));
}
void build_tree(ll start,ll end,ll index){
   if (start==end){
      segment_tree[index]=a[i];
      i++;
      return;
   }
   ll mid=(start+end)/2;
   build_tree(start,mid,2*index);
   build_tree(mid+1,end,2*index+1);
   segment_tree[index]=gcd(segment_tree[2*index],segment_tree[2*index+1]);
}
ll get_gcd(ll l,ll r,ll start,ll end,ll index){
   if (end<l || start>r){
      return 0;
   }
   if (l<=start && end<=r){
      return segment_tree[index];
   }
   ll mid=(start+end)/2;
   //cout<<mid<<endl;
   return (gcd(get_gcd(l,r,start,mid,2*index),get_gcd(l,r,mid+1,end,2*index+1)));
}
bool is_possible(ll len){
   ll start_ptr=0;
   ll end_ptr=len;
   vll v;
   //cout<<len<<endl;
   //cout<<n<<endl;
   while (start_ptr<n){
      //cout<<start_ptr<<" "<<end_ptr<<endl;
      ll g=get_gcd(start_ptr,end_ptr,0,2*n-1,1);
      //cout<<g<<endl;
      v.pb(g);
      start_ptr++;
      end_ptr++;
   }
   if (vmin(v)==vmax(v)){
      return true;
   }
   else{
      return false;
   }
}
void solve(){
   cin>>n;
   ll o=0;
   rep(0,n,1){
      ll x;
      cin>>x;
      a[o]=x;
      o++;
   }
   rep(0,n-1,1){
      a[o]=a[j];
      o++;
   }
   rep(0,4*n+1,1){
      segment_tree[j]=0;
   }
   build_tree(0,2*n-1,1);
   ll start=0;
   ll end=n-1;
   while (1){
      if (end-start==1){
         bool x,y;
         x=is_possible(start);
         y=is_possible(end);
         if (x && y){
            cout<<start<<endl;
            return;
         }
         cout<<end<<endl;
         return;
      }
      ll mid=(start+end)/2;
      bool x,y;
      x=is_possible(mid);
      y=is_possible(mid+1);
      if (!x && !y){
         start=mid+1;
         continue;
      }
      if (x && y){
         end=mid;
         continue;
      }
      if (!x && y){
         cout<<mid+1<<endl;
         return;
      }
      break;
   }
}
int main(){
   fast;
   mt19937 rand(chrono::steady_clock::now().time_since_epoch().count());
   ll t=1;
   cin>>t;
   while (t--){
      solve();
      //a.clear();
      i=0;
      
   }
   return 0;
}