Suppose $G$ is a group such that for each $a$ and $b$ in $G$, $(ab)^3=a^3b^3$ and $(ab)^5=a^5b^5.$ We know that in such a condition, $G$ is an abelian group (the proof is here).
My question is this: is there any generalization for this? If so, what's the proof of that? more specifically, is there any rule for when $(ab)^p=a^p b^p$ and $(ab)^q= a^q b^q$ with a certain relation between $p$ and $q$ (possibly relating to their gcd) we can say $G$ is abelian?