for w in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    if(n==1 or sorted(a)==sorted(list(set(a)))):
        print(-1)
    elif(len(set(a))==1):
        print(2)
    else:
        d={}
        for i in range(n):
            if(a[i] not in d):
                d[a[i]]=[i]
            else:
                d[a[i]].append(i)
        l1=list(d.keys())
        l2=list(d.values())
        ans=n
        for i in range(len(l2)):
            for j in range(len(l2[i])-1):
                ans=min(ans,l2[i][j+1]-l2[i][j]+1)
        print(ans)