N = int(2e5+3)
adj = [[] for _ in range(N)]
t = int(input())
for _ in range(t):
    n = int(input())
    arr = list(map(int, input().split()))
    for i in range(n+1):
        adj[i] = []
    for i in range(n):
        adj[arr[i]].append(i)
    res = int(2e9)
    for i in range(n+1):
        res = min([res]+[adj[i][j] - adj[i][j-1] + 1 for j in range(1, len(adj[i]))])
    print(-1 if res == int(2e9) else res)

 							  				  	 					      	