for _ in range(int(input())):
    n = int(input())
    li = list(map(int,input().split()))
    count_dict = {}
    flag = True
    for i in range(n):
        if count_dict.get(li[i]) == None:
            count_dict[li[i]] = (i,n)
        else:
            val = i - count_dict[li[i]][0] + 1
            flag = False
            if val < count_dict[li[i]][1]:
                count_dict[li[i]] = (i,val)
            else:
                count_dict[li[i]] = (i,count_dict[li[i]][1])
    if flag:
        print(-1)
    else:
        li = list(count_dict.values())
        length = len(li)
        mini = li[0][1]
        for i in range(1,length):
            if mini > li[i][1]:
                mini = li[i][1]
        print(mini)