t = int(input())
while t!=0:
    t-=1
    n = int(input())
    arr = list(map(int,input().split(' ')))
    lst = [-1 for i in range(n+1)]
    ans = n+5
    for i in range(n):
        if lst[arr[i]] != -1:
            ans  = min(ans,i - lst[arr[i]]+1)
        lst[arr[i]] = i
    if ans>n:
        print(-1)
    else:
        print(ans)

