from collections import defaultdict
for _ in range(int(input())):
    n = int(input())
    l = list(map(int,input().split()))
    d = defaultdict(int)
    m = 10**10
    for i,j in enumerate(l):
        if d[j]==0:
            d[j]=i+1
        else:
            m = min(m,i+1-d[j]+1)
            d[j]=i+1
    if m==10**10:
        print(-1)
    else:
        print(m)