#1257C
for _ in range(int(input())):
    n = int(input())
    ans = n + 1
    d = {}
    for x,y in enumerate(map(int, input().split())):
        if y in d:
            ans = min(ans, x-d[y] + 1)
        d[y] = x
    print(-1 if ans >n else ans)
