import sys
from collections import defaultdict
def get(arr):
    n=len(arr)
    if n==1:
        return -1
    ans=float('inf')
    dic=defaultdict(int)
    l,r=0,0
    rem=-1
    #em=-1
    while l<n:
        #print(l,'l',r,'r',ans,'ans',dic)
        while r<n and dic[rem]<2:
            #print(r,'check')
            dic[arr[r]]+=1
            #rem=arr[r]
            if dic[arr[r]]==2:
                ans=min(ans,r-l+1)
                rem=arr[r]
                r+=1
                break
            r+=1
        #print(dic,'dic',l,'l',r,'r')
        dic[arr[l]]-=1
        l+=1
        if dic[rem]>=2:
            ans=min(ans,r-l)
        #print(dic,'dic after',l,'l',r,'r')
        #l+=1
    if ans==float('inf'):
        return -1
    return ans
t=int(sys.stdin.readline())
for _ in range(t):
    n=int(sys.stdin.readline())
    arr=list(map(int,sys.stdin.readline().split()))
    print(get(arr))
