for  _  in range(int(input())):
    n = int(input())
    co = [[-1,10**6] for i in range(n+1)]
    l = list(map(int,input().split()))
    s= set(l)
    
    if(len(s)==n):
        print(-1)
    else:
        for i in range(n):
           
            if(co[l[i]][0]==-1):
                co[l[i]][0] = i
                co[l[i]][1] = 10**6
            else:
                co[l[i]][1] = min(co[l[i]][1],i - co[l[i]][0])
                co[l[i]][0] = i

        ans = 10**6
    
        for i in range(n+1):
            ans = min(ans,co[i][1])
        print(ans+1)



