def solve (n,seq) :
    table = {}
    distance = n + 1
    index = 0
    
    while index < n :
        if seq[index] not in table :
            table[seq[index]] = index
        else :
            dis2 = (index - table[seq[index]])+1
            if dis2 < distance :
                distance = dis2
            table[seq[index]] = index
        index += 1
    

    if distance > n :
        return -1
    return distance
    
    
for x in range(int(input())):
    n = int(input())
    seq = list(map(int,input().split()))
    print (solve(n,seq))
    