t=int(input())
count=210
for i in range(t):
    n=int(input())
    arr=list(map(int,input().split()))
    if len(arr)==1:
        print(-1)
    elif len(arr)==2:
        if arr[0]==arr[1]:
            print(2)
        else:
            print(-1)
    else:
        i = 0
        j = n-1
        while i<n-1 and j>i:
            if arr[j] == arr[i]:
               if len(set(arr[i:j]))==j-i:
                   count=min(count,j+1-i)
                   j-=1
               else:
                   j-=1
            else:
                j-=1
            if j==i:
                j=n-1
                i+=1
        print(count)