for _ in range(int(input())):
    n = int(input())
    L = input().split()
    P = {}
    ans = 1e18
    for i in range(len(L)):
        if(L[i] in P.keys()):
            ans = min(ans,i - P[L[i]] + 1)
        P[L[i]] = i
    print(-1 if ans == 1e18 else ans)


	      		 	   			 		    				  	