from collections import defaultdict
t = int(input())
for query in range(t):
    n = int(input())
    a = list(map(int, input().split()))
    d = defaultdict(list)
    s = {}
    gans = float('Infinity')
    for i in range(n):
        d[a[i]].append(i)
        if a[i] not in s:
            s[a[i]] = float('Infinity')
    for elem in d:
        if len(d[elem])>=2:
            k = d[elem]
            for j in range(1, len(d[elem])):
                s[elem] = min(abs(k[j]-k[j-1]), s[elem])
            gans = min(s[elem], gans)
    if gans<float('Infinity'):
        print(int(gans)+1)
    else:
        print(-1)