Answer
$5000\;\rm J$
Work Step by Step
Let's assume that the system is perfectly isolated, so the heat loss from gas A is gained by gas B. And hence the total thermal energy of the two gases is constant.
After enough time the two gases will have the same temperature.
Hence,
$$\Delta E_{th,A}=n_AC_{\rm v}\Delta T_A$$
Hence,
$$\Delta T_A=\dfrac{\Delta E_{th,A}}{n_AC_{\rm v}}$$
Plugging the known;
$$\Delta T_A=\dfrac{-1000}{4(12.5)}=\bf -20\rm\; K$$
By the same approach:
$$\Delta T_B=\dfrac{\Delta E_{th,B}}{n_BC_{\rm v}}$$
$$\Delta T_B=\dfrac{1000}{3(12.5)}=\bf 26.67\rm\; K$$
We can find the initial temperature of gas A,
$$(E_{i,th})_A=\frac{3}{2}n_ART_{iA}$$
Hence,
$$T_{iA}=\frac{2}{3}\dfrac{(E_{i,th})_A}{n_AR}=\frac{2}{3}\dfrac{ 9000}{(4)(8.31)}$$
$$T_{iA}=\bf 180.5\;\rm K$$
And hence its final temperature after the system reach equilibrium is
$$\Delta E_{th,A}=-1000=n_AC_{\rm v}(T_{f}-T_{iA})$$
Thus,
$$T_{f}=\dfrac{-1000}{(4)(12.5)}+180.5=\bf 160.5\;\rm K$$
The final temperature of gas B is also 180.5 K, hence its initial temperature is then
$$\Delta T_B=T_f-T_{iB}$$
$$T_{iB}=T_f-\Delta T_B=160.5-26.67\approx 134\;\rm K$$
Thus the initial thermal energy of gas B is given by
$$(E_{i,th})_B=\frac{3}{2}n_BRT_{iB}$$
Plugging the known;
$$(E_{i,th})_B=\frac{3}{2} (3) (8.31) (134)$$
$$(E_{i,th})_B=\color{red}{\bf 5000}\;\rm J $$