tc = int(input())
for ii in range(tc):
    n = int(input())
    ar = list(map(int,input().split()))
    if n==1:
        print(-1)
        continue
    x = dict()
    m = float('inf')
    c = 0
    for i in range(n):
        if ar[i] in x:
            dif = i-x[ar[i]]
            m = min(m,dif)
            c = 1
            x[ar[i]] = i
        else:
            x[ar[i]] = i
    if c==0:
        print(-1)
    else:
        print(m+1)
            
