t = int(raw_input())
for i in range(t):
    n = raw_input()
    array = raw_input().split(' ')
    lens = {}
    counts = {}
    lastIds = {}
    id = 0
    for num in array:
        if num in counts:
            counts[num] += 1
            cur_len = id - lastIds[num]
            if lens[num] > 0:
                lens[num] = min(cur_len, lens[num])
            else:
                lens[num] = cur_len
            lastIds[num] = id
        else:
            counts[num] = 1
            lens[num] = 0
            lastIds[num] = id
        id += 1

    rest = [v for k, v in lens.iteritems() if counts[k] > 1]
    if len(rest) > 0:
        print min(rest) + 1
    else:
        print "-1"

