Need help with a question

what i tried was keep doing prime factorization of elements in A and store prime in a hashmap, do this for B and store its primes in another hashmap.
have a counter and loop through A and for every number check if its prime factor is present in the other arrays hashmap, if its present increment counter.
do this for B and now the minimum of the counter for A and B will be the number of special pairs because of pigeonhole principle.
Got tle and wa in most tc. Can anyone point out whats wrong or point in the correct direction? thank you