to understand the problem and the basis behind free body diagram check the following three FBDs of sytem, Mass A and B and finally the rope.
The net force on the system is Fnet = (F0-Mg)
Accorging to Newtons Second law for a system of mass M; Fnet =Ma
Thus acceleration of system will be a = (F-Mg)/M.
A body will accelerate in a given direction if there is any net force in that direction.
Note here that the top of the rope is pulling the 5Kg mass downward with a force Ttop (i.e. tension at the top of the rope) At the same time the rope is pulling the 3Kg mass with a force Tbottom (i.e. tension at the bottom)
Two points to note here:
1. In this problem , as the joint system of all three masses is acceleration with some acceleration ‘a’ each FBD should give a result that each mass is also moving with same acceleration This would be better explained by numerical values if force F0 was known
2. Also note the directions of tension forces by rope in each case as the rope can only pull and not push (unless it’s a rigid rod).
Newtons Third law: There exist equal and opposite reaction force for every action force. And the reaction force acts on the adjacent body in contact.
It is can be easily seen that Ttop is greater than not only Tbottom but the sum of (Tbottom and 2g)
Additionally the combination of all the FBDs of subsystems of a bigger system must result into FBD of the system itself. Check the same for yourself.
Note that I have assumed that rope is perfectly rigid otherwise problem requires completely different treatment.
Now moving up if rope was massless Ttop = Tbottom. Tension throughout the rope would be same. That value then can be obtained by applying the Second Law on any of the two FBDs given above.