for _ in range(int(input())):
    n,a  =  (int(input()) , input().split())
    d = n; seen = {};
    for i,a in enumerate(a):
        if(a in seen):
            d  = min(d , i - seen[a])
        seen[a] = i
    print(d+1 if d < n else -1)