t = int(input())
for T in range(t):
    n = int(input())
    s = [int(i) for i in input().split()]
    if n == 1:
        print('-1')
    else:
        length = n
        a = [-1]*(n+1)
        for i in range(n):
            if (a[s[i]] != -1) and (i - a[s[i]] + 1) < length:
                length = i - a[s[i]] + 1
            a[s[i]] = i
        print(length)
