Element A has 3 electrons in outermost shell so its valency is 3. It will loose 3 electrons to attain stability, as loss of 3 electrons is easier than gain of 5 electrons.
Valency of B is −2 as it will gain 2 electrons to attain stability and combine with other atom.