q=int(input())
for i in range(q):
    w=int(input())
    e=list(map(int,input().split()))
    if w>1:
        r=dict()
        ans=999999999999999999
        j=0
        for i in e:
            if i in r:
                ans=min(ans,j-r[i]+1)
                r[i]=j
            else:
                r[i]=j
            j+=1
        print(ans if ans!=999999999999999999 else -1)
    else:print(-1)