Let A have x pencils and let B have y pencils.
When A gives 10 pencils to B, he is left with x−10 pencils and B has y+10 pencils.
The first condition is y+10=2(x−10).
If B gives 10 pencils to A, then B has y−10 and A has x+10 pencils. The second condition says
y−10=x+10.
Thus we get two equations
2x−y=30.....(1)
y−x=20.......(2)
Adding the equations (1) and (2), we obtain x=50.
Using the equation (2), we obtain y=x+20=50+20=70.
Thus A has 50 pencils and B has 70 pencils.