for T in range(int(input())):
    n = int(input())
    s = input().split()
    if n < 2:
        print(-1)
        continue

    occ = [-1] * (n+1)
    d_min = 2*10**5 + 1
    t = []

    for i in range(n):
        v = int(s[i])
        if occ[v] != -1:
            d = i - occ[v] + 1
            if d < d_min:
                d_min = d
        occ[v] = i
        
    print(d_min)

