def minSize(arr):
    ans=-1
    hashmap=dict()
    for i in range(len(arr)):
        if arr[i] in hashmap:
            lastIndex=hashmap[arr[i]]
            length=i-lastIndex+1
            if ans==-1 or length<ans:
                ans=length
        hashmap[arr[i]]=i;
    return ans;

numTestCases=int(input(''))
while numTestCases>0:
    n=int(input(''))
    arr=input('').strip().split()
    for i in range(len(arr)):
        arr[i]=int(arr[i]);
    print(minSize(arr))
    
    numTestCases-=1