[Improve] the solution to sherlock_and_queries.

Find solution from "Editorial" section.