rearrange query