import sys
ip=lambda :sys.stdin.readline().rstrip()
from collections import *
for _ in range(int(ip())):
    n=int(ip())
    a=list(map(int,ip().split()))
    d={}
    ans=float('inf')
    for i in range(n):
        if d.get(a[i],-1)!=-1:
            ans=min(ans,i-d[a[i]]+1)
        d[a[i]]=i
    if ans==float('inf'):
        print(-1)
    else:
        print(ans)