Solution (source code)

= Solution

Under the null, replace both parameters by a common $r$. The likelihood becomes
$$
L_0(r)=\binom{2N}n\binom{2M}m
r^{n+m}(1-r)^{2(N+M)-n-m}.
$$
The pooled maximum-likelihood estimate is
$$
\boxed{\widehat r=\frac{n+m}{2(N+M)}.}
$$
Under an interior null $p=q=r$, part (ii) gives a centered asymptotic normal difference with <variance> $r(1-r)(1/(2M)+1/(2N))$. The <law of large numbers> makes $\widehat r\to r$ in probability. By Slutsky's theorem, replacing $r$ by this consistent estimate gives
$$
\boxed{Z=\frac{\widehat p-\widehat q}
{\sqrt{(1/(2M)+1/(2N))\widehat r(1-\widehat r)}}\xrightarrow dN(0,1).}
$$
For the two-sided alternative, reject when $|Z|>z_{1-\alpha/2}$, the corresponding standard-normal <quantile>. This has asymptotic <significance level> $\alpha$.