While moving from left to right in a periodic table, the chemical reactivity of the elements decreases from sodium to silicon and then it increases from phosphorus to chlorine. Electronic configuration of metal A is given as 2,8,1, (same as the electronic configuration for sodium metal) while that of metal B is 2,8,2 (the electronic configuration of magnesium metal). Meanwhile, the first element of the third period, sodium has 1 valence electron, which it can lose easily to react with other substances. For this reason, sodium is a highly reactive metal. The second element, magnesium has 2 valence electrons, which is not so easy to lose. For this reason, magnesium is less reactive than sodium.
Reactions with dilute hydrochloric acid:
Na + HCl → NaCl + H2
Mg + HCl → MgCl2 + H2