t = int(input())
for _ in range(t):
    n=int(input())
    A = list(map(int,input().split()))
    if n==1:
        print(-1)
        continue
    d = dict()
    res = n
    for i in range(n):
        if A[i] in d:
            res = min(res,i-d[A[i]]+1)
        d[A[i]]=i
    if len(d)==n:
        print(-1)
    else:
        print(res)