t = int(input())
for i in range(t):
    n = int(input())
    lst = list(map(int,input().split()))
    
    Dict = {}
    ans = float('inf')
    
    for i in range(n):
        ch = lst[i]
        if(Dict.get(ch)==None):
            Dict[ch]= [i]
        else:
            ans = min(ans,i-Dict[ch][-1])
            Dict[ch].append(i)
    if(ans == float('inf')):
        print("-1")
    else:
        print(ans+1)