INF = int(1e9)

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

    print(ans)