t = int(input())
for _ in range(t):
    n = int(input())
    adjacentes = []
    for i in range(n + 1):
        adjacentes.append([])

    entrada = list(map(int, input().split(" ")))
    a = []
    for i in range(n):
        a.append(entrada[i])
        adjacentes[a[i]].append(i + 1)

    total = 200001
    for i in range(n + 1):
        if len(adjacentes[i]) > 1:
            for j in range(len(adjacentes[i]) - 1):
                k = adjacentes[i][j + 1] - adjacentes[i][j] + 1
                total = min(total, k)


    if total == 200001:
        print(-1)
    else:
        print(total)
    print()
   		 	 				 	 	 			  	  	 	 	