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