t = int(input())
for i in range(t):
    ans = []
    d = {}
    n = int(input())
    l = [int(s) for s in input().split()]
    for j in range(n):
        if l[j] in d:
            ans.append(j - d[l[j]] + 1)
        d[l[j]] = j
    if ans != []:
        print(min(ans))
    else:
        print(-1)
