from collections import defaultdict as dc
import sys
input=sys.stdin.readline
for _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    x=dc(list)
    f=0
    m=sys.maxsize
    for i in range(n):
        if x[l[i]]:
            p=i-x[l[i]][-1]+1
            if p<m:
                m=p
                f=1
        x[l[i]].append(i)
    if f:
        print(m)
    else:
        print(-1)