#import random
for i in range(int(input())):
    n = int(input())
    a = [int(x) for x in input().split()]

    #n = 1000
    #a = [int(random.random() * 1000000) for x in range(n)]

    if n == 1:
        print(-1)
        continue
    boolean = False
    b = a.copy()
    b.sort()
    for j in range(len(b) - 1):
        if b[j] == b[j + 1]:
            boolean = True
            break
    if not boolean:
        print(-1)
        continue
    b = [-1] * (max(a) + 5)
    ans = n + 5
    for i in range(n):
        if b[a[i]] != -1:
            ans = min(ans, i - b[a[i]] + 1)
        b[a[i]] = i
    print(ans)
