from collections import defaultdict
t = int(input())
for i in range(t):
  n = int(input())
  d = defaultdict(list)
  a = list(map(int,input().split()))
  for i in range(n):
    d[a[i]].append(i)
  m = n+1
  for i in d.values():
    for j in range(1,len(i)):
      m = min(m,i[j]-i[j-1]+1)
  if m != n+1:
    print(m)
  else:
    print(-1)