n=int(input())
for i in range(n):
    x=int(input())
    xlist=[int(x) for x in input().split(' ')]
    if len(xlist) == 1:
        print('-1')
    else:
        xdic={}
        d=0
        for i in range(x):
            if str(xlist[i]) not in xdic:
                xdic[str(xlist[i])]=i
            else:
                c=xdic[str(xlist[i])]
                if d == 0:
                    d=i-c
                    xdic[str(xlist[i])]=i
                else:
                    d=min(d,i-c)
                    xdic[str(xlist[i])]=i
            if d == 1:
                break
        if d == 0:
            print('-1')
        else:
            print(d+1)
