t = int(input())

for case in range(t):
    n = int(input())
    l = [int(x)-1 for x in input().split()]
    last_seen = [-1 for x in range(n)]
    min_dist = [n for x in range(n)]
    for i in range(n):
        if last_seen[l[i]] != -1:
            min_dist[l[i]] = min(min_dist[l[i]], i - last_seen[l[i]])
        last_seen[l[i]] = i

    # print(last_seen)
    if (min(min_dist) == n):
        print(-1)
    else:
        print(min(min_dist)+1)

 				 	 	  			 						 	 		 				