I'd do it the other way around. The query parameters may contain slashes, but the path cannot contain question marks. If you try to reduce directories first, you will have to resolve the ambiguity of slashes in the path vs slashes in the query parameters. If you remove the query parameters first, for which there is an unambiguous criterion, then the slashes suddenly are unambiguous too.
Makeshifts last the longest.