for _ in range(int(input())):
   n = int(input())
   arr = list(map(int,input().split()))
   d = {}
   ans = n+1
   for i in range(n):
       if(arr[i] not in d):
           d[arr[i]] = i
       else:
           ans = min(ans , i-d[arr[i]]+1)
           d[arr[i]] = i
   if(ans != n+1):
        print(ans)
   else:
        print(-1)    
