for _ in range(int(input())):
    n = int(input())
    d = dict()
    a = list(map(int, input().split()))
    for i in range(n):
        if not d.get(a[i]):
            d[a[i]] = [float('inf'), i]
        elif i - d[a[i]][1] + 1 < d[a[i]][0]:
            d[a[i]] = [i - d[a[i]][1] + 1, i]
    ans = min(d.values())[0]
    if ans == float('inf'):
        print(-1)
    else:
        print(ans)
