test = int(input())
pos = [-1] * 200005
for c in range(test):
    n = int(input())
    a = list(map(int,input().split()))
    for x in a:
        pos[x] = -1
    len = n+5
    for i in range(n):
        if(pos[a[i]]==-1): pos[a[i]] = i
        else:
            len = min(len,i-pos[a[i]]+1)
            pos[a[i]] = i
    if(len==n+5 or len == 1):   len = -1
    print(len)