t = int(input())
while(t > 0):
    t -= 1
    n = int(input())
    a = list(map(int,input().split()))
    if len(a) <= 1:
        print(-1)
        continue
    d = {}
    for i in range(n):
        if a[i] in d:
            d[a[i]][0] += 1
            d[a[i]].append(i)
        else:
            d[a[i]] = [1,i]
    res = []
    for i in d:
        if d[i][0] > 1:
            for j in range(2,len(d[i])):
                tmp = d[i][j] - d[i][j-1]
                res.append(tmp+1)
    try:
        print(min(res))
    except:
        print(-1)
    