t=int(input())
for _ in range(t):
  n=int(input())
  li=list(map(int,input().split()))
  ans=n
  dic={}
  for i in range(n):
    val=dic.get(li[i],-1)
    if val==-1:
      dic.update({li[i]:i})
    else:
      ans=min(ans,i-val+1)
      dic.update({li[i]:i})
  if len(dic)==n:
    print(-1)
  else:
    print(ans)  