t = int(input().strip())
for case in range(t):
    occ = {}
    n = int(input().strip())
    ls = list(map(int, input().strip().split()))
    ans = 999999
    for i in range(n):
        if not ls[i] in occ.keys():
            occ[ls[i]] = i
        else:
            if ans > i - occ[ls[i]]:
                ans = i - occ[ls[i]];
            occ[ls[i]] = i 
    if ans == 999999:
        print('-1')
    else:
        print(ans+1)