for j in range(input()):
    subArrayIndexes = {}
    size = input()
    if size == 1:
        discard = input()
        print -1
    else:
        arr = map(int,raw_input().split())
        head,tail = 0,1
        for i in range(size-1):
            if arr[head] not in subArrayIndexes.keys():
                subArrayIndexes.update({arr[head]:[0,200000]})
            if arr[tail] == arr[head]:
                subArrayIndexes[arr[head]][0] = head + 1
                subArrayIndexes[arr[head]][1] = tail + 1
                if head-tail == 1:
                    break
                head = tail
            tail+=1
        sortedDict = sorted(subArrayIndexes.items(),key = lambda k: k[1][1] - k[1][0])
        print sortedDict[0][1][1] - sortedDict[0][1][0] + 1


