import math
for i in range(int(input())):
n=int(input())
values=list(map(int,input().split()))
values2=[]
for k in range(1001):
values2.append([0])
l=0
while l<n:
values2[values[l]].append(l+1)
values2[values[l]][0]+=1
l+=1
values1=[]
value=[0]*1001
for val in values:
value[val]+=1
g=0
while g<1001:
if value[g]>0:
values1.append(g)
g+=1
le=len(values1)
j=0
flag=0
ma=0
curr=[]
nxt=[]
b=0
while j<le :
k=j
while k<le:
if math.gcd(values1[j],values1[k])==1:
curr.append(values1[j])
nxt.append(values1[k])
b+=1
flag=1
k+=1
j += 1
if flag==0:
print(-1)
else:
c=0
while c<b:
ch1=values2[curr[c]][0]
ch2=values2[nxt[c]][0]
ma=max(ma,values2[curr[c]][ch1]+values2[nxt[c]][ch2])
c+=1
print(ma)
#include <bits/stdc++.h>
using namespace std;
int main()
{
long long t;
cin>>t;
while(t--)
{
long long n;
cin>>n;
vector<int> a(1001);
for(int i=1;i<=n;i++)
{
int temp;
cin>>temp;
a[temp]=i;
}
long long max= -1;
for(long long i=1;i<=1000;i++)
{
for(long long j=1;j<=1000;j++)
{
if(a[i] && a[j] && ((int)__gcd(i,j)==1))
{
if(max<(a[i]+a[j]))
{
max=a[i]+a[j];
}
}
}
}
cout<<max<<endl;
}
}
518. Coin Change 2 | 516. Longest Palindromic Subsequence |
468. Validate IP Address | 450. Delete Node in a BST |
445. Add Two Numbers II | 442. Find All Duplicates in an Array |
437. Path Sum III | 436. Find Right Interval |
435. Non-overlapping Intervals | 406. Queue Reconstruction by Height |
380. Insert Delete GetRandom O(1) | 332. Reconstruct Itinerary |
368. Largest Divisible Subset | 377. Combination Sum IV |
322. Coin Change | 307. Range Sum Query - Mutable |
287. Find the Duplicate Number | 279. Perfect Squares |
275. H-Index II | 274. H-Index |
260. Single Number III | 240. Search a 2D Matrix II |
238. Product of Array Except Self | 229. Majority Element II |
222. Count Complete Tree Nodes | 215. Kth Largest Element in an Array |
198. House Robber | 153. Find Minimum in Rotated Sorted Array |
150. Evaluate Reverse Polish Notation | 144. Binary Tree Preorder Traversal |