import math def are_coprime(a, b): return math.gcd(a, b) == 1 def find_greatest(): input() alist = [int(a) for a in input().split()] mapping = {} for i in range(len(alist)): mapping[alist[i]] = i maxi = -1 for a in mapping: for b in mapping: if mapping[a]+mapping[b]+2 > maxi and are_coprime(a, b): maxi = mapping[a]+mapping[b]+2 return maxi def main(): nb = int(input()) for i in range(nb): print(find_greatest()) main()