t = int(input())
for i in range(t):
    n = int(input())
    a = [int(j) for j in input().split(" ")]
    if(n==1):
        print(-1)
    else:
        small = 1000000
        m = [-1 for j in range(n)]
        for j in range(n):
            #print(j,a[j],m[a[j]-1])
            if(m[a[j]-1]==-1):
                m[a[j]-1] = j
            else:
                
                if(small>(j-m[a[j]-1]+1)):
                    small = j-m[a[j]-1]+1
                    m[a[j]-1] = j
        print(small)
    
