for _ in range(int(input())):
    n=int(input())
    arr=[int(k) for k in input().split()]
    dic={}
    for i in range(n):
        if arr[i] not in dic:
            dic[arr[i]]=[i]
        else:
            dic[arr[i]].append(i)
    if len(dic)==n:
        print("-1")
    else:
        
        mini=n+1
        for i,j in dic.items():
            if len(j)>1:
                j.sort()
                for k in range(len(j)-1):
                    sumi=(j[k+1]-j[k])+1
                    mini=min(sumi,mini)
        print(mini)
                