A solution using simple functions:
=RIGHT(CONCAT(OFFSET(Sheet2!$B$2;0;XMATCH($A2;Sheet2!$B$1:$D$1)-1;1000));5)
The only things that you need to change are
The formula is for the cell B2, but you can copy to the other cells in column B