import math
t=int(input())
for _ in range(t):
    n=int(input())
    a=list(map(int,input().split()))
    d={}
    ans=math.inf
    for i in range(n):
        if a[i] not in d:
            d[a[i]]=i
        else:
            ans=min(ans,i-d[a[i]]+1)
            d[a[i]]=i
    if ans==math.inf:
        print(-1)
    else:
        print(ans)