t=int(input())
for _ in range(t):
    n=int(input())
    s=[int(x) for x in input().split()]
    pos=[]
    for i in range(n+2):
        h=[]
        pos.append(h)

    for i in range(0,len(s)):
        pos[s[i]].append(i)

    temp=n+100
    for i in range(0,len(pos)):
        for j in range(1,len(pos[i])):
            temp=min(temp,pos[i][j]-pos[i][j-1]+1)

    if(temp==n+100):
        print(-1)
    else:
        print(temp)
            
        
