amount = int(input())
for i in range(amount):
    n = int(input())
    array = [int(s) for s in input().split()]
    counter = [[] for i in range(n + 1)]
    ans = 200001
    if n < 2:
        print(-1)
        continue
    for i in range(len(array)):
        counter[array[i]].append(i)
    #print(counter)
    for i in range(1, len(counter)):
        if len(counter[i]) < 2:
            continue
        else:
            for j in range(1, len(counter[i])):
                ans = min(ans, counter[i][j] - counter[i][j - 1] + 1)
    if ans == 200001:
        print(-1)
    else:
        print(ans)
                    