t=int(input())
from collections import defaultdict as dd
while t:
    d=dd(list)
    n=int(input())
    mi=100000000000000000000
    l=list(map(int,input().split()))
    lol=0
    for i in range(n):
        #print(d[l[i]])
        if(len(d[l[i]])!=0):
            lol=1
            if((i-d[l[i]][0])<mi):
                mi=(i-d[l[i]][0])
            d[l[i]][0]=i
        else:
            d[l[i]].append(i)
    if(lol==0):
        print(-1)
    else:
        print(mi+1)
    t-=1