for _ in range(int(input())):
    n=int(input())
    s=list(map(int,input().split()))
    min_len=n+1
    ind_list=[]
    for i in range(n):
        ele=[]
        ele.append(s[i])
        ele.append(i)
        ind_list.append(ele)
    ind_list.sort()
    for i in range(n-1):
        if ind_list[i][0]==ind_list[i+1][0]:
            diff=ind_list[i+1][1]-ind_list[i][1]
            diff+=1
            if min_len>diff:
                min_len=diff
    if min_len==n+1:
        print('-1')
    else:
        print(min_len)
    
   