for _ in range(int(input())):
    n = int(input())
    l = [int(x) for x in input().split()]
    m = [-1] * (n + 1)
    cnt = 987654321
    
    for x in range(n):
        if m[l[x]] != -1:
            if cnt > x - m[l[x]]:
                cnt = x - m[l[x]]
        m[l[x]] = x

    if cnt == 987654321:
        print(-1)
    else:
        print(cnt + 1)