for _ in range(int(input())):
    n = int(input())
    arr = list(map(int, input().split()))
    cnt = [0] * (n + 1)
    id_last = [-1] * (n + 1)
    len_min = [n + 1] * (n + 1)
    id_last[arr[0]] = 0
    len_best = n + 1
    left = 0
    for i in range(1, n):
        #print(i, arr[i], id_last[arr[i]])
        if id_last[arr[i]] != -1:
            len_min[arr[i]] = min(len_min[arr[i]], i - id_last[arr[i]] + 1)
            len_best = min(len_best, len_min[arr[i]])
        id_last[arr[i]] = i
    if len_best == n + 1:
        print(-1)
    else:
        print(len_best)
