import math
t = int(input())
while(t):
    n = int(input())
    l = list(map(int, input().split()))
    c=n
    c1=0
    d={}
    for i in range(n):
        try:
            if(d[l[i]][0]>=1):
                d[l[i]][0]+=1
                c = min(c,abs(d[l[i]][-1]-i)+1)
                d[l[i]].append(i)
                c1=1
        except KeyError:
            d[l[i]] = [1,i]
    if(c1==1):
        print(c)
    else:
        print(-1)
    t-=1