from collections import *
for _ in range(int(input())):
    n = int(input())
    l = [int(i) for i in input().split(' ')]
    if n == 1:
        print(-1)
        continue
    ma = max(Counter(l).values())
    if ma == 1:
        print(-1)
        continue
    visited = {}
    ans = n
    for i in range(n):
        if l[i] in visited:
            ans = min(ans,i - visited[l[i]])
        visited[l[i]] = i
    print(ans+1)
