def solution(arr):
    d = {}
    for i in range(len(arr)):
        val = d.get(arr[i],[])
        if len(val) == 0 :
            d[arr[i]] =[]
        d[arr[i]].append(i)
    n_arr = list(d.values())
    l = []
    for i in n_arr :
        l.append(len(i))
    val = max(l)
    mn = len(arr)
    for i in n_arr :
        if len(i) == val :
            for j in range(val-1):
                cal = i[j+1] - i[j] + 1 
                mn = min(mn,cal)
    return mn 
    
# arr = [int(x) for x in input().split()]
# print(solution(arr))
for _ in range(int(input())):
    n = int(input())
    arr = [int(x) for x in input().split()]
    if len(arr) == 1 :
        print(-1)
    else:
        print(solution(arr))