5. Let the two real roots of $x^{2}-p x+q=0$ be $\alpha, \beta$, and the quadratic equation with $\alpha^{2}, \beta^{2}$ as roots is still $x^{2}-p x+q=0$. Then the number of pairs $(p, q)$ is ( ).
(A) 2
(B) 3
(C) 4
(D) 0
5. (B).

From the problem,
$\left\{\begin{array}{l}\alpha+\beta=p, \\ \alpha \beta=q\end{array}\right.$ and $\left\{\begin{array}{l}\alpha^{2}+\beta^{2}=p, \\ \alpha^{2} \beta^{2}=q .\end{array}\right.$
Thus, $q^{2}=q$. Therefore, $q=0$ or $q=1$.
And we have $(\alpha+\beta)^{2}=\alpha^{2}+\beta^{2}+2 \alpha \beta$, which means $p^{2}=p+2 q$.
(1) When $q=1$, $p=2$ or $p=-1$;
(2) When $q=0$, $p=0$ or $p=1$.
But $\alpha^{2}+\beta^{2}=p \geqslant 0$, so $p=-1$ (discard).
Therefore, the pairs $(p, q)$ that satisfy the problem can be $(2,1),(0,0)$, $(1,0)$, a total of 3 pairs.