# dominated_subarray.py
from collections import defaultdict
for _ in range(int(input())):
    n = int(input())
    a = list(map(int,input().split()))
    mydick = dict()
    mydick = defaultdict(lambda:list(),mydick)
    ans = 10e7
    ok = False
    for i in range(n):
        mydick[a[i]].append(i)
        if len(mydick[a[i]])>1:
            p = len(mydick[a[i]])
            ans = min(mydick[a[i]][p-1]-mydick[a[i]][p-2],ans)
            ok = True

    if ok:
        print(ans+1)
    else:
        print(-1)
