for i in range(int(input())):
    input()
    M = [int(n) for n in input().split()]
    D = dict()
    for i in range(len(M)):
        e = M[i]
        if e not in D:
            D[e] = []
        D[e].append(i)
    L = [x for x in D.values() if len(x) > 1]
    if not L: print(-1)
    else:
        m = L[0][1] - L[0][0]
        for lis in L:
            for i in range(len(lis) - 1):
                if (lis[i+1] - lis[i]) < m:
                    m = lis[i+1] - lis[i]
        print(m+1)