import math

t = int(input())
while t:
    n = int(input())
    s = input().split(" ")
    a = [0]
    v = [[]]
    for i in range(0,n):
        a.append(int(s[i]))
        v.append([])
    # print(a)
    for i in range(1,n+1):
        v[a[i]].append(i)
    ok = 0
    ans = 200005
    for i in range(1,n+1):
        if len(v[i]) > 1:
            ok = 1
        for j in range(0,len(v[i])-1):
            ans = min(ans, v[i][j+1] - v[i][j])
    
    if ok == 0:
        print(-1)
    else:
        print(ans+1)
    t -= 1