for _ in range(int(input())):
    mydict = dict()
    
    n = int(input())
    a = list(map(int, input().split()))
    
    min_ = 100000
    
    for i in range(n):
        prev = mydict.get(a[i])
        if prev is not None:
            min_ = min(min_, i-prev+1)
        mydict[a[i]] = i
    
    print(-1 if min_ == 100000 else min_)
        
    