for i in range(int(input())):
    n = int(input())
    l=list(map(int,input().split()))
    mini=n
    s=set(l)
    d = {}
    if len(s)==n:
        print(-1)
        continue
    for i in range(n):
        try:
            if mini>i-d[l[i]]:
                mini= i-d[l[i]]+1
            d[l[i]]=i
        except:
            d[l[i]]=i
    print(mini)