t = int(input())
l = []
for i in range(t):
    n = int(input())
    array0 = input().split()
    array1 = array0.copy()
    l0 = [n]
    for j in range(n):
        l1 = []
        mid = j
        lm = min([j+min(l0),n])
        for k in range(j+1, lm):
            if array0[j] == array0[k]:
                l1.append(k-mid+1)
                mid = k
        if mid != j:
            l0.append(min(l1))
    if len(l0) != 0:
        l.append(min(l0))
    else:
        l.append(-1)
for p in range(t):
    print(l[p])