#t = int(input())
#l = [int(i) for i in input().split()]


t = int(input())
for j in range(t):
    n = int(input())
    l = [int(i) for i in input().split()]
    hashmap = [[]]
    for i in range(n):
        hashmap.append([])
    for i in range(n):
        hashmap[l[i]].append(i)
    flag = 0
    for i in range(1,n+1):
        if (len(hashmap[i])>1):
            flag=1
            break
    if (flag==0):
        print (-1)
    else:
        min1 = 100000000
        for i in range(1,n+1):
            l1 = hashmap[i]
            length = len(l1)
            if (length>1):
                for k in range(length-1):
                    if (l1[k+1]-l1[k]<min1):
                        min1 = l1[k+1]-l1[k]
        print (min1+1)




