How to change pagination base from slash to query form?

I am not entirely sure what you are asking but that ?p= pattern is a raw query string. That is, that is what you get without pretty permalinks at all.

Go to Settings->Permalinks in wp-admin and select the default permalinks. Save and you are done. You don’t need any other function. If that doesn’t solve it you need to rewrite your question so that it is more clear.