t = int(input())

for i in range(t):
    l = int(input())
    s = input().split()
    d = {}
    for j in range(0, l):
        diff = 200001
        if s[j] not in d:
            d[s[j]] = [j, diff]
        else:
            diff = min(j - d[s[j]][0], d[s[j]][1])
            d[s[j]] = [j, diff]
    my_list = []
    my_iter = (iter(d.values()))
    for i in my_iter:
        my_list.append(i[1])
    my_list.sort()
    z = my_list[0]
    if z == 200001: print(-1)
    else: print(z+1)