t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    ans = n + 5
    lst = [-1]*(n+1)
    for i in range(n):
        if lst[a[i]] != -1:
            ans = min(ans, i-lst[a[i]]+1)
        lst[a[i]] = i
    if ans > n:
        ans = -1
    print(ans)