Pour heat Q into the gas and let it push the piston out, doing work W. Whatever heat is left over after paying for the work stays inside as a rise in internal energy ΔU. Slide the two inputs and watch the balance — it never breaks.