t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    s={}
    l=[]
    for i in range(n):
        if a[i] not in s:
            s[a[i]]=i
        else:
            l.append(i-s[a[i]]+1)
            s[a[i]]=i
    if len(l)==0:
        print(-1)
    else:
        print(min(l))