Context free grammar for $L = \{u\#v \mid u,v \in \{a,b\}^* , \vert u \vert_a \neq \vert v \vert_a \text{ or } \vert u \vert_b \neq \vert v \vert_b\}$
I'll just show how to build a grammar for  $L_1 = \{ u\#v, |u|_a &gt; |v|_a \}$. Then it'll be straightforward to combine 4 similar grammars into a grammar for $L$.

The idea is to write $u\#v$ as $xay\#v$ with $x,y,v \in \{a,b\}^*$ and $|y|_a = |v|_a$.  The construction of $y\#v$ is handled by the non-terminal $Z$ which "grows" it from the center.

$$
\begin{align*}
S &amp;\to aS \mid bS \mid aZ \\
Z &amp;\to bZ \mid Zb \mid aZa | \,\#
\end{align*}
$$