t = int(input())
for _ in range(t):
    n = int(input())
    a = [int(i) for i in input().split()]
    if n < 2:
        print(-1)
    elif n == len(set(a)):
        print(-1)
    else:
        last = {}
        ans = n
        for i in range(n):
            if a[i] in last:
                ans = min(ans, i - last[a[i]] + 1)
            last[a[i]] = i
        print(ans)