for i in range(int(input())):
    n = int(input())
    a = [int(x) for x in input().split()]
    anser = []
    d = {}
    for j in range(n):
        if a[j] in d:
            anser.append(j-d[a[j]]+1)
        d[a[j]] = j
    if anser == []:
        print(-1)
    else:
        print(min(anser))
    
        
