#!/usr/bin/python3

t=int(input())
for i in range(t):

    n=int(input())
    dic={}
    a=[]
    m=-1
    if(n==1):
        tmp=int(input())
        print("-1")
    else:
        #Convertimos a lista al leer del map para que sea un array
        a = list(map(int, input().split()))
        #print(a)
        for i in range(len(a)):
            if a[i] not in dic:
                dic[a[i]]=i
            else:
                k=i-dic[a[i]]+1
                dic[a[i]]=i
                if m==-1 or k<m: m=k
        print(m)