def solve(arr):
  idxs = dict()
  n = len(arr)
  for i in range(0, n):
    v=arr[i]
    if not v in idxs:
      idxs[v] = [i]
    else:
      lv=idxs[v]
      lv[len(lv):] = [i]
      
  minRange = n
  for key in idxs:
    lv=idxs[key]
    for i in range(1, len(lv)):
      minRange = min(minRange, lv[i]-lv[i-1])
      
  print(minRange+1 if minRange != n else -1)

for _ in range(0, int(input())):
  n = int(input())
  arr = list(map(int, input().split()))
  solve(arr)