t=int(input())
for _ in range(t):
    dic={}
    n=int(input())
    temp=list(map(int,input().split(" ")))
    for x in temp:
        dic[x]=[]

    i=0
    while i<n:
        dic[temp[i]].append(i)
        i+=1
    res=30000000000
    for x in dic:
        i=0
        while i<len(dic[x])-1:
            if dic[x][i+1]-dic[x][i]<res:
                res=dic[x][i+1]-dic[x][i]

            i+=1
    if res==30000000000:
        print(-1)
    else:
        print(res+1)

