for _ in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    if n==1:
        print('-1')
    else:
        m=10**6
        for j in range(1,n):
            for i in range(j-1,-1,-1):
                if a[j]==a[i]:
                    m=min(m,j-i+1)
                    if m==2:
                        break
        print(m)
