from collections import Counter

T = int(input())

for t in range(T):
    n = int(input())
    a = input().split(' ')  #оставляем строкой

    count_a = Counter(a) #num, occ
    dist, last_index = {}, {}

    for i in range(n):
        if count_a[str(a[i])] > 1:
            if not (str(a[i]) in dist):  #встречаем элемент впервые
                last_index[str(a[i])] = i
                dist[str(a[i])] = int(0)
            else:
                if dist[str(a[i])] == 0:
                    dist[str(a[i])] = i - last_index[str(a[i])]
                else:
                    dist[str(a[i])] = min(i - last_index[str(a[i])], dist[str(a[i])])
                last_index[str(a[i])] = i
    #answer
    if len(dist) == 0:
        print(-1)
    else:
        print(min(dist.values()) + 1)