def solve(v):
  
  if len(v) < 2:
    return -1
  
  last = dict()
  
  best = len(v) + 1
  
  for i in range(len(v)):
    if v[i] in last:
      best = min(best, i - last[v[i]] + 1)
    last[v[i]] = i

  return best if best <= len(v) else -1
 
if __name__ == '__main__':
  t = int(input())
  
  for i in range(t):
    input()
    v = list(map(int, input().split()))
    print(solve(v))