t = int(input())

for _ in range(t):
    n = int(input())
    a = list(map(int,input().split()))
    if len(set(a))==n:
        print(-1)
        continue
    table = {}
    for i in a:
        table[i] = []
    possible = set()
    for i in range(n):
        e = a[i]
        table[e].append(i)
        if len(table[e])>=2:
            possible.add(e)
    ans = 999999999
    for i in possible:
        table[i].sort()
        for j in range(0,len(table[i])-1):
            ans = min(ans,table[i][j+1]-table[i][j]+1)
    print(ans)