for _ in range(int(input())):
    n=int(input())
    a=[int(i) for i in input().split()]
    s={}
    b=[]
    for i in range(n):
        if a[i] in s:
            b.append(i-s[a[i]]+1)
            s[a[i]]=i
        else:
            s[a[i]]=i
    if len(b)==0:
        print(-1)
    elif len(b)==1:
        if b[0]>1:
            print(b[0])
        else:
            print(-1)
    else:
        print(min(b))
            