import collections
for t in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    counter=dict()
    if len(set(arr))==n:
        print(-1)
        continue
    ans=10**10
    for i in range(n):
        ele=arr[i]
        if ele in counter:
            diff=i-counter[ele]+1
            ans=min(diff,ans)
            counter[ele]=i
        else:
            counter[ele]=i
    print(ans)