t=int(input())
for i in range(t):
    n=int(input())
    arr=list(map(int,raw_input().split()))

    def check(arr,n):
        ans=2*(10**5)
        pos=[None]*(n+1)
        for i in range(1,n+1):
            if pos[arr[i-1]]==None:
                pos[arr[i-1]]=i
            else:
                ans=min(ans,i-pos[arr[i-1]]+1)
                pos[arr[i-1]]=i
        if ans==2*(10**5):
            return -1
        else:
            return ans


    print (check(arr,n))
