t=int(input())
while t>0:
    n=int(input())
    arr=list(map(int,input().split()))
    m = [[]for _ in range(n+1)]
    for i in range(1,n+1):
        if len(m[arr[i-1]])<2:
            m[arr[i-1]].append(i)
            if len(m[arr[i-1]])==2:
                d = m[arr[i-1]][1]=m[arr[i-1]][1]-m[arr[i-1]][0]
                m[arr[i-1]][0]=i
                m[arr[i-1]][1]=d
        else:
            m[arr[i-1]][1]=min(m[arr[i-1]][1],i-m[arr[i-1]][0])
            m[arr[i-1]][0]=i
    flag=0
    mini=1000000
    for i in range(1,n+1):
        if len(m[i])>1:
            flag=1
            mini = min(m[i][1],mini,m[i][0])
    if flag==0:
        print("-1")
    else:
        # print(m[4][0])
        print(mini+1)
    t-=1