for _ in range(int(input())):
    n=int(input())
    s=list(map(int,input().split()))
    f = {}; m = 10**6
    if n==1 or len(set(s))==n:
        print('-1')
    else:
        for i in range(n):
            if s[i] not in f: f[s[i]] = i 
            else: 
                m = min(m, i - f[s[i]]+1)
                f[s[i]] = i
        print(m)
    
    