t=int(input())
for i in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    if len(l)==len(set(l)):
        print(-1)
    else:
        dic={}
        mindiff=300000
        for i in range(n):
            if l[i] in dic:
                mindiff=min(mindiff,i-dic[l[i]])
            dic[l[i]]=i
        print(mindiff+1)
