# maa chudaaye duniya
for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    last_seen = {}
    min_diff = 10**10
    if n < 2:
        print(-1)
        continue
    else:
        for i in range(n):
            if arr[i] in last_seen:
                min_diff = min(min_diff, i-last_seen[arr[i]]+1)
                last_seen[arr[i]] = i
            else:
                last_seen[arr[i]] = i
        if min_diff == 10**10:
            print(-1)
        else:
            print(min_diff)