test=int(input())
for testing in range(test):
    n=int(input())
    t=[int(x) for x in input().split()]
    if n<=1 :
        print(-1)
        continue
    else :
        pointer=[[False,0] for i in range(max(max(t),len(t))+1)]
        first=True
        mini=-1
        for i in range(len(t)):
            if not pointer[t[i]][0]:
                pointer[t[i]][1]=i
                pointer[t[i]][0]=True
                continue
            if pointer[t[i]][0]:
                if first :
                    mini=i-pointer[t[i]][1]+1
                    first=False
                else:
                    a=i-pointer[t[i]][1]+1
                    if mini>a:
                        mini=a
            pointer[t[i]][1]=i
            pointer[t[i]][0]=True
            if mini==2:
                break
    print(mini)
