import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
T = int(input())
for _ in range(T):
    n = int(input())
    arr = map(int,input().split(" "))
    dic = [-1]*(n+1)
    minv = n+1
    for i,v in enumerate(arr):
        if dic[v]!= -1:
            minv = min(minv,i-dic[v])
        dic[v] = i
    if minv == n+1:
        print -1
    else:
        print minv+1

