t=int(input())
for i in range(t):
    n=int(input())
    l=list(map(int,input().split()))
    x=n+1
    p=[-1]*(n+1)
    for j in range(n):
        if p[l[j]]!=-1:
            x=min(x,j-p[l[j]]+1)
        p[l[j]]=j
    if x>n:
        x=-1
    print(x)
