t = int(input())
while(t):
    t -= 1
    n = int(input())
    a = list(map(int, input().split()))
    d = dict()
    for i in range(n):
        if(a[i] in d):
            d[a[i]].append(i)
        else:
            d[a[i]] = [i]
    mini = n+n
    for i in d:
        b = d[i]
        for x in range(1, len(b)):
            mini = min(mini, b[x] - b[x-1] + 1)
    if(mini == n+n):
        print(-1)
    else:
        print(mini)