'''Author- Akshit Monga'''
from sys import stdin,stdout
input = stdin.readline
t=int(input())
for _ in range(t):
    n=int(input())
    arr=[int(x) for x in input().split()]
    dict={}
    for i in range(n):
        if arr[i] in dict:
            dict[arr[i]].append(i)
        else:
            dict[arr[i]]=[i]
    ans=float('inf')
    for i in dict:
        for j in range(len(dict[i])-1):
            ans=min(ans,dict[i][j+1]-dict[i][j]+1)
    if ans==float('inf'):
        ans=-1
    print(ans)