[guided]We now check that the displayed formula really gives the equality case, rather than only guessing it from the completed square. Define the map $\lambda:\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
\lambda_d := \mu(d)\prod_{p\mid d}(1-g(p))^{-1}\frac{G_d(D,z)}{G(D,z)}.
\end{align*}
For $d=1$, the product over primes is the empty product and $G_1(D,z)=G(D,z)$, so
\begin{align*}
\lambda_1=1.
\end{align*}
Thus $\lambda$ is a Selberg weight of level $D$.
Fix $r\in\mathcal{S}_+$. The condition $h(r)>0$ means every prime divisor of $r$ satisfies $g(p)>0$, so the factors $1-g(p)$ are non-zero. We compute the diagonal variable
\begin{align*}
x_r(\lambda)=\sum_{d\in\mathcal{S},\ r\mid d}\lambda_d g(d).
\end{align*}
Every divisor $d\in\mathcal{S}$ with $r\mid d$ has a unique form $d=ra$, where $a<D/r$, $(a,r)=1$, and $a\mid P(z)$. Since $r$ and $a$ are coprime squarefree divisors, multiplicativity of $\mu$, $g$, and $h$ gives
\begin{align*}
\lambda_{ra}g(ra)=\frac{\mu(r)\mu(a)h(r)h(a)G_{ra}(D,z)}{G(D,z)}.
\end{align*}
Therefore
\begin{align*}
x_r(\lambda)=\frac{\mu(r)h(r)}{G(D,z)}\sum_{a<D/r,\ (a,r)=1,\ a\mid P(z)}\mu(a)h(a)G_{ra}(D,z).
\end{align*}
Now expand the definition of $G_{ra}(D,z)$:
\begin{align*}
G_{ra}(D,z)=\sum_{m<D/(ra),\ (m,ra)=1,\ m\mid P(z)}h(m).
\end{align*}
Substituting this expression, the inner sum becomes
\begin{align*}
\sum_{a<D/r,\ (a,r)=1,\ a\mid P(z)}\sum_{m<D/(ra),\ (m,ra)=1,\ m\mid P(z)}\mu(a)h(a)h(m).
\end{align*}
The coprimality condition $(m,ra)=1$ ensures in particular that $(a,m)=1$, so multiplicativity gives $h(a)h(m)=h(am)$. Grouping by $n=am$ gives
\begin{align*}
\sum_{n<D/r,\ (n,r)=1,\ n\mid P(z)}h(n)\sum_{a\mid n}\mu(a).
\end{align*}
For squarefree $n$, the Möbius divisor sum is
\begin{align*}
\sum_{a\mid n}\mu(a)=\prod_{p\mid n}(1-1),
\end{align*}
which equals $1$ for $n=1$ and $0$ for $n>1$. Hence the grouped sum equals $1$, and we obtain
\begin{align*}
x_r(\lambda)=\frac{\mu(r)h(r)}{G(D,z)}.
\end{align*}
Multiplying by $\mu(r)$ and using $\mu(r)^2=1$ for squarefree $r$ gives
\begin{align*}
y_r(\lambda)=\frac{h(r)}{G(D,z)}.
\end{align*}
This is exactly the equality condition from the completed-square step for every $r\in\mathcal{S}_+$. Consequently the displayed weights attain
\begin{align*}
Q(\lambda)=\frac{1}{G(D,z)}.
\end{align*}[/guided]