import sys, math
from heapq import heappush, heappop
readline = sys.stdin.readline
mr = lambda:map(int,readline().split())
t = int(readline())

for _ in range(t):
    n = int(readline())
    tmp = list(mr())
    tp = [[] for _ in range(n)]
    asnArr = [0 for _ in range(n)]
    for i in range(n):
        tp[tmp[i] - 1].append(i)
    for i in range(n):
        if len(tp[i]) > 1:
            d = tp[i][1] - tp[i][0]
            for j in range(len(tp[i]) - 1):
                if tp[i][j + 1] - tp[i][j] < d:
                    d = tp[i][j + 1] - tp[i][j]
            asnArr.append(d)
    if n == 1 or len(asnArr) == 0:
        print(-1)
    else:
        ans = n + 1
        for i in range(len(asnArr)):
            if asnArr[i] != 0 and ans > asnArr[i]:
                ans = asnArr[i]
        print(ans + 1)
