Hi.
In Chemistry, we always see energy gain or loss from the perspective of the System.
The total Change in Energy of a system = Energy gained or lost as Heat + Energy gained or lost as Work.
Total energy change is +40 J
Heat gained is +30 J
So, 40 = 30 + Work
Work = 10 J
Since W = 10 J is positive, this means that the system gained 10 J of energy as work. When a system GAINS energy in the form of work, it means work was done ON the system BY the surroundings. The system did NOT do work ON the surroundings.
If the work had been negative -- for example, -20 J -- then we can say that the system did work ON the surroundings; and from the systems perspective, it lost 20 J of energy as work.
Hope that helps. Best wishes.