$(x-a)^2(x-b) = x^3+px+q$

Multiply out:

$(x-a)^2(x-b) = (x^2-2ax+a^2)(x-b) = x^3-(2a+b)x^2+(2ab+a^2)x-a^2b$

Equate coefficients:

$-(2a+b) = 0$

$2ab+a^2=p$

$-a^2b = q$

This is a system of three equations in four variables. So, solve for $p$ in terms of $q$.