t = int(input())
while t != 0:
    n = int(input())
    l = list(map(int, input().strip().split()))
    dic = {}
    for index, i in enumerate(l):
        if i not in dic:
            dic[i] = list()
            dic[i].append(index)
            continue
        dic[i].append(index)
    mini = 999999999
    flag = False
    for i in dic:
        lx = list()
        lx = dic[i]
        if len(lx) != 1:
            flag = True
            for x in range(0, len(lx)-1):
                mini = min(mini, lx[x+1]-lx[x])
    if flag: print(mini+1)
    else: print(-1)
    t -= 1
