import sys
from collections import defaultdict
t = int(input())
for kkk in range(t):
    n = int(sys.stdin.readline())
    a = list(map(int ,sys.stdin.readline().split()))
    d = defaultdict(int)
    m = n+1
    for i in range(len(a)):
        if d[a[i]] == 0:
            d[a[i]] = i+1
        else:
            m = min(m, i - d[a[i]] + 2)
            d[a[i]] = i+1
    if m == n+1:
        print(-1)
    else:
        print(m)