from collections import defaultdict
t = int(input())
for _ in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    count = {i:[] for i in a}
    for i,v in enumerate(a):
        count[v].append(i)
    # print(count)

    ans = n + 1
    for uniq_num, occur in count.items():
        # print(uniq_num, occur)
        if len(occur) >= 2:
            m = len(occur)
            for i in range(1, m):
                ans = min(ans, occur[i]-occur[i-1])
            # print(ans)
    if ans == n+1:
        print(-1)
    else:
        print(ans+1)
