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

                b[a[j]-1]=j
                
        '''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)
