t = int(input())
for T in range(t):
    n = int(input())
    s = [int(i) for i in input().split()]
    length = n+1
    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
    if length > n:
        length = -1
    print(length)
