from collections import defaultdict

T = int(input())
RES_MAX = 2 * 10**5 + 1

for t in range(T):
    n = int(input())
    a = list(map(int, input().split()))

    if n == 1:
        print(-1)
    else:
        d = {}
        res = 2 * 10**5 + 1
        for i in range(n):
            if a[i] in d:
                res = min(res, i-d[a[i]]+1)
                d[a[i]] = i
            else:
                d[a[i]] = i
        if res < RES_MAX:
            print(res)
        else:
            print(-1)
