t = int(input())
for i in range(t):
  sub = []
  ok =False
  n =int(input())
  a = list(map(int,input().split()))
  d={}
  res=[]
  o=0
  for e in set(a):
    d[e]=[]
  for i in range(len(a)):
    d[a[i]].append(i)
  for k in d:
    if len(d[k])>=2:
      for j in range(len(d[k])-1):
        res.append(d[k][j+1]-d[k][j]+1)
    else:
      o +=1
  if o==len(a):
    print(-1)
  else:
    print(min(res))