from collections import Counter,defaultdict
t=int(input())
while(t):
    t-=1
    n=int(input())
    *a,=map(int,input().split())
    if Counter(a).most_common(1)[0][1]==1:
        print(-1)
        continue
    pos={}
    ans=int(1e9)
    for i in range(n):
        if pos.get(a[i]) is None:
            pos[a[i]]=i
        else:
            ans=min(ans,i-pos[a[i]]+1)
            pos[a[i]]=i
    print(ans)