T = int(input())
for t in range(T):
    n = int(input())
    a = [int(x)-1 for x in input().split()]
    if n==1:
        print(-1)
    else:
        w = [[] for x in range(n)]
        for i in range(n):
            w[a[i]].append(i)
        m = n
        for i in range(n):
            b = w[i]
            l = len(b)
            if l>1:
                for j in range(l-1,0,-1):
                    b[j] -= b[j-1]
                M = min(b[1:l])
                if M<m:
                    m=M
        if m == n:
            print(-1)
        else:
            print(m+1)