finalized the code for traversing the query automaton (explain structure)