import sys
input = sys.stdin.readline


t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    li = {}
    for i in range(n):
        if a[i] not in li:
            li[a[i]] = [i]
        else:
            li[a[i]].append(i)
    ans = 10**9
    for num in li:
        tmp = li[num]
        for j in range(len(tmp) - 1):
            ans = min(ans, tmp[j+1] - tmp[j] + 1)
    if ans == 10**9:
        print(-1)
    else:
        print(ans)