for ttt in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    if len(set(arr))==n:
        print(-1)
    else:
        for i in range(n-1):
            if arr[i]==arr[i+1]:
                print(2)
                break  
        else:
            for i in range(n-2):
                a,b,c=arr[i],arr[i+1],arr[i+2]
                if a==b  or b==c  or a==c:
                    print(3)
                    break 
            else:
                print(-1)