for _ in range(int(input())):
    
    n = int(input())
    s = list(map(int,input().split()))
    
    a = list()
    
    b = list()
    for i in range(n+1):
        b.append(-1)
    c = list()
    for i in range(n+1):
        c.append(-1)
    a.append(b)
    a.append(c)
    
    
    for i in range(n):
        
        f = s[i]
        #print(f)
        if(a[0][f]==-1):
            a[0][f] = i
        else:
            g = i-a[0][f]+1
            if(a[1][f]==-1):
                a[1][f] = g
            else:
                a[1][f] = min(a[1][f],g)
        a[0][f] = i
                
    ans = 1000000
    
    for i in range(1,n+1):
        if(a[1][i]!=-1):
            ans = min(ans,a[1][i])
    #print(a)    
    if(ans == 1000000):
        print(-1)
    else:
        print(ans)