from  collections import  defaultdict
from math import inf
for _ in range(int(input())):
    n=int(input())
    arr=list(map(int,input().split()))
    d=defaultdict(lambda:-1)
    m=inf
    for i in range(n):
        if d[arr[i]]!=-1:
            m=min(i-d[arr[i]]+1,m)
        d[arr[i]]=i
    if m==inf:
        print(-1)
    else:
        print(m)


