T=int(input())
for _ in range(T):
    n=int(input())
    nums=list(map(int,input().split()))
    if len(nums)==1:print(-1);continue
    minn=2*10**5+1
    d={}
    for i in range(n):
        if nums[i] not in d.keys():
            d[nums[i]]=i
        else:
            minn=min(minn,abs(i-d[nums[i]])+1)
            d[nums[i]]=i
    if len(d)==n:
        print(-1)
    else:print(minn)
