import sys
input = lambda: sys.stdin.readline().strip()
print = lambda s: sys.stdout.write(s)

t = int(input())
for i in range(t):
    n = int(input())
    ls = list(map(int, input().split()))
    d = {}
    for i in ls:
        d[i] = []
    for i in range(n):
        d[ls[i]].append(i)
    ans = []
    for k, v in d.items():
        v.sort()
        for i in range(len(v)-1):
            ans.append(v[i+1]-v[i])
    try:
        res = min(ans)+1
    except:
        res = -1
    res = res if res>=2 else -1
    print(str(res)+'\n')
