rewrite with query monoid