What is the relationship between the five elements, earthly branches and heavenly stems mentioned in the name?

The corresponding relationship between the five elements and the heavenly stems:

Wood → A and B, fire → Bing and Ding, earth → Wu and Ji, metal → Geng and Xin, water → Ren and Gui.

The corresponding relationship between the Five Elements and the Earthly Branches:

Wood → Yinmao, Fire → Siwu, Earth → Chenchou Xuwei, Metal → Shenyou, Water → Haizi.