for _ in range(int(input())):
    n = int(input())
    a = [int(__) for __ in input().split()]
    l1 = [-1 for ___ in range(n)]
    ans = n + 1
    for i in range(n):
        if l1[a[i] - 1] != -1:
            if i - l1[a[i] - 1] + 1 < ans:
                ans = i - l1[a[i] - 1] + 1
        l1[a[i] - 1] = i
        if ans == 2:
            break
    if ans == n + 1:
        print(-1)
    else:
        print(ans)
