t = int(input())
for tt in range(t):
    n = int(input())
    ent = list(map(int,input().split()))
    mn = 1000000000
    arr = [-1]*(n+1)
    for i in range(n):
        if arr[ent[i]] == -1:
            arr[ent[i]]=i
        else:
            mn = min(mn,i-arr[ent[i]])
            arr[ent[i]]=i
    if mn==1000000000:
        print(-1)
    else:
        print(mn+1)


