def solution(arr) :
    if len(arr) == 1 :
        return -1 
    d = {}
    for i in arr :
        d[i] = d.get(i,0) + 1 
    v = max(list(d.values()))
    n_arr = list(d.values())
    count = 0 
    for i in n_arr :
        if i == v :
            count = count + 1 
#     print(n_arr)
    if count > 1 :
        return -1 
    for i in d :
        if d[i] == v :
            ele = i 
            break 
    pos = []
    for i in range(len(arr)):
        if arr[i] == ele :
            pos.append(i)
#     print(pos)
    length = []
    for i in range(v-1):
        length.append(pos[i+1]-pos[i])
    return min(length) + 1 
# arr = list(map(int,input().split()))
# print(solution(arr))
for _ in range(int(input())):
    n = int(input())
    arr = [int(x) for x in input().split()]
    print(solution(arr))
    
# 4
# 1
# 1
# 6
# 1 2 3 4 5 1
# 9
# 4 1 2 4 5 4 3 2 1
# 4
# 3 3 3 3
