Now recall that in a quadratic equation
\begin{equation*}
x^2 - b x + c = 0,
\end{equation*}
the coefficient $b$ is the sum of the two roots, and $c$ is their product. It follows that you can find $p$ and $q$ as the roots of the equation
\begin{equation*}
x^2 - (n + 1 - \varphi(n)) x + n = 0.
\end{equation*}