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)

    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):
                mini= diff

    
    if(mini == 0):
        print(-1)
    else:
        print(mini +1)
    # print(mini+1)
    # print(start)
    # print(end)
    # print(count)