for _ in range(int(input())):
    x = int(input())
    a = [*map(int,input().split())]
    mini = x
    start =[0]*(x+1)
    end = [0]*(x+1)
    count = [0]*(x+1)
    st = 0
    ele = 0
    en = 0

    for i in range(x):
        el = a[i]
        count[el] +=1
        if(count[el]%2 ==1 ):
            end[el] = i
        else:
            start[el] = i
        if(count[el] >=2):
            diff = abs(start[el]-end[el])
            if(mini > diff):
                ele = el
                st = start[el]
                en = end[el]
                mini= diff

    
    if(count[ele] > 1):
        print(mini +1)
    else:
        print(-1)
    