We want to find $a_1$ and $a_2$ using only $p$ and $q$

pq_visualization

$$ x^2 - \underbrace{(a_1+a_2)}_{p}x + \underbrace{a_1a_2}_{q} = (x-a_1)(x-a_2). $$

With

$$ q= a_1a_2= (m-d)(m+d) = m^2 - d^2, $$

we immediately get the p-q formula

$$ \begin{aligned} a_{1/2} &= m \pm d \\ &= m \pm \sqrt{m^2 - q}\\ &= \frac{p}{2} \pm \sqrt{\left(\frac{p}{2}\right)^2 - q} \end{aligned} $$

Source: 3blue1brown