test = int(input())
for t in range(test):
    n = int(input())
    a = list(map(int,input().split()))
    occurence = {}
    min_length = 10**8
    for i in range(n):
        try:
            occurence[a[i]].append(i)
            temp = occurence[a[i]][-1] - occurence[a[i]][-2] + 1
            if temp < min_length:
                min_length = temp
        except KeyError:
            occurence[a[i]] = [i]
    if min_length != 10**8:
        print(min_length)
    else:
        print(-1)