t=int(input())
a=[]
for i in range(t):
    n=input()
    b=list(map(int,input().split()))
    a.append(b)
for i in a:
    if len(i)==1:
        print(-1)
    else:
        j=0
        m=1000000000
        k=0
        o=len(i)
        p=o
        while(j<o):
            try:
                l=0
                if j<p:
                    for h in range(j + 1, p):
                        if i[h] == i[j]:
                            l = 1
                            p = h
                            break
                else:
                    for h in range(j + 1, o):
                        if i[h] == i[j]:
                            l = 1
                            p = h
                            break



                if l==1 and (p-j)<m:
                    k=1
                    m=(p-j)
            except:
                pass
            if m==1:
                break
            j+=1
        if k==0:
            print(-1)
        else:
            print(m+1)






