for _ in [0]*int(input()):
    n=int(input())
    d=[[-1]*2 for i in range(n+1)]


    s=list(map(int,input().split()))
    if n == 1: print(-1)
    else:
        short = n + 1
        for i in range(n):
            if d[s[i]][0]==-1:
                d[s[i]][0]=i
            else:
                d[s[i]][1]=i-d[s[i]][0]+1
                d[s[i]][0]=i
                short=min(short,d[s[i]][1])
        if short==n+1:print(-1)
        else:print(short)
