##for debug comment out
import sys,atexit
from io import BytesIO
inp = BytesIO(sys.stdin.buffer.read())
input = lambda:inp.readline().decode('ascii')
buf = BytesIO()
#sys.stdout.write = lambda s: buf.write(s.encode('ascii'))
#print = lambda s: buf.write(s.encode('ascii'))
atexit.register(lambda:sys.__stdout__.buffer.write(buf.getvalue()))

for i in range(int(input())):
    n=int(input())
    a=list(map(int,input().split()))
    g=[[] for i in range(n+1)]

    for i in range(n):
        g[a[i]].append(i)
    ans=10**7
    f=False
    for i in range(1,n+1):
        if len(g[i])>1:
            f=True
            for j in range(1,len(g[i])):
                ans=min(ans,g[i][j]-g[i][j-1]+1)
    if f:
        print(ans)
    else:
        print(-1)