import sys
from collections import defaultdict
for _ in range(int(input())):
    d = defaultdict(int)
    n = int(input())
    a = list (sys.stdin.readline().split())
    mx = int(2e5)
    top = 0
    r=0
    l=-1
    ret = int(1e6)
    while(r<n):
        cur = a[r]
        d[cur]+=1
        c = 0
        while d.get(cur) > 1:
            ret = min(ret, (r - l))
            l += 1
            remove = a[l]
            d[remove] = d.get(remove)- 1
            c+=1
        r+=1

    sys.stdout.write(str(ret) if ret<=mx else '-1')
    sys.stdout.write('\n')
