import math,sys
from collections import Counter, defaultdict, deque
#for deque append(),pop(),appendleft(),popleft(),count()
from sys import stdin, stdout
input = stdin.readline
lili=lambda:list(map(int,sys.stdin.readlines()))
li = lambda:list(map(int,input().split()))
I=lambda:int(input())
S=lambda:input()

t=I()
for i in range(0,t):
    n=I()
    a=li()
    d=defaultdict(list)
    for i in range(0,n):
        d[a[i]].append(i)
    m=10**6
    l,r=0,0
    for i in d:
        if(len(d[i])==1):
            continue
        for j in range(0,len(d[i])-1):
            f=d[i][j+1]-d[i][j]
            m=min(m,f)
    if(m==10**6):
        print(-1)
    else:
        print(m+1)


