t=int(input())
for i in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    if(len(a)==1):
        print(-1)
    elif(len(set(a))==1):
        print(2)
    else:
        arr=[]
        allLen=[]
        for i in range(len(a)):
            if(a[i] not in arr):
                arr.append(a[i])
            else:
                len1=len(arr)+1
                allLen.append(len1)
                arr=[]
                arr.append(a[i])
        print(min(allLen))
