Suppose we have a Lagrangian function $$L(\lambda, q) = \sum_{i=1}^{n}q(i)[\log\pi(u_i)+\log U(r_i) - \log q(i) + \lambda] - \lambda.$$
By using Lagrange multiplier we can find optimal $q(j)$.
The solution is $q(j) = \frac{\pi(u_j)U(r_j)}{\sum_{i=1}^{n}\pi(u_i)U(r_i)}$.
But it is quite strange to me, since by the standard procedure, we compute gradients $\frac{\partial L}{\partial \lambda}, \frac{\partial L}{\partial q}$, I got
$\begin{align} \frac{\partial L}{\partial \lambda} &= \big[\sum_{i=1}^{n}q(i)\big] - 1 = 0\\ \frac{\partial L}{\partial q(j)} &= \log\pi(u_j) + \log U(r_j) - \log q(j) - 1 + \lambda = 0 \end{align}$
Question: Then it is hard for me to continue, because it does not lead to the solution above.
Note: Here $q(i)$ is a probability distribution i.e. the constraint part. And $\pi(\cdot), U(\cdot)$ are just some functions depending on each iteration $i$
From
$$\frac{\partial L}{\partial q(j)}=0$$
We have $$\lambda-1 = \log \frac{q(j)}{U(r_j)\pi(u_j)}$$
which is equivalent to $$\exp(\lambda-1) = \frac{q(j)}{U(r_j)\pi(u_j)}$$
That is regardless of value of $j$, $\frac{q(j)}{U(r_j)\pi(u_j)}$ is a positive constant, or we can say that $q(j)$ is proportional to $U(r_j) \pi (u_j)$.
Also, we know that $q(j)$ sums to $1$ and they are non-negative.
Hence, $$q(j)=\frac{\pi(u_j)U(r_j)}{\sum_{i=1}^n\pi(u_i)U(r_i)}$$