import math

def revolt(l,n):
    d={}
    min_=math.inf
    for i in range(n):
        if d.get(l[i],-1)>=0:
            min_=min(i-d[l[i]]+1,min_)
            d[l[i]]=i
        else:
            d[l[i]]=i
    if min_>n:
        return -1
    return min_
            



for _  in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    print(revolt(l,n))