for _ in range(int(input())):
    n=int(input())
    ai=list(map(int,input().split()))
    index=[[] for q in range(n+2)]
    value=[]
    for i in range(n):
        index[ai[i]].append(i)
        if len(index[ai[i]])==2:
            value.append(index[ai[i]][1]-index[ai[i]][0])
            index[ai[i]].pop(0)
    try:
        e=(min(value))
        if e==10000000:
            print(-1)
        else:print(e+1)
    except ValueError:print(-1)