from collections import defaultdict
def ab():
    b=int(input())    
    c=list(map(int,input().split(" ")))
    q=[]
    bc=defaultdict(list)
    min=b+1
    if(b==1):
        print("-1")
        return
    else:
        for i in range(0,b):
            bc[c[i]].append(i)
        for x in bc:
            for r in range(0,len(bc[x])-1):
                w=abs(bc[x][r]-bc[x][r+1])
                if(min>w):
                    min=w
    if(min>b):
        print("-1")
    else:
        print(min+1)
           
a=int(input())
for i in range(0,a):
    ab()