t = int(input())
for T in range(0,t):
    mp = {}
    array = []
    n = int(input())
    l = list(map(int, input().split()))
    for i in range(0, n):
        if l[i] in mp:
            array.append(i - mp[l[i]] + 1)
        mp[l[i]] = i
    if array == []:
        print(-1)
    else:
        print(min(array))
