import sys
for _ in range(int(input())):
  n=int(input())
  arr=list(sys.stdin.readline().split())
  ans=10**9
  if n==1:
    print(-1)
  else:
   d=dict()
   for i in range(n):
     if arr[i] in d:
       l=d[arr[i]]
       ans=min(ans,i-l[0]+1)
       d[arr[i]].append(i)
    
     else:d[arr[i]]=[i]
   
   if ans==10**9:
      print(-1)
   else:
      print(ans)
