
def diff(pair):
    return(abs(pair[0]-pair[1]))

t = int(input())

for i in range(t):
    n = int(input())
    dic = {}
    values = list(map(int, input().split(" ")))
    for i, elem in enumerate(values):
        try:
            index_pair = dic[elem]
            if len(dic[elem]) == 1:
                dic[elem].append(i)
            elif diff(index_pair) > diff([dic[elem][1], i]):
                dic[elem].append(i)
                dic[elem].remove(0)
        except:
            dic[elem] = [i]

    mini = len(values)
    flag = 1
    for i in dic.values():
        if len(i) == 2 and diff(i) < mini:
            flag = 0
            mini = 1 + diff(i)
    if flag:
        print(-1)
    else:
        print(mini)

    
        
