in S5
=INDEX($C$5:$N$36,MATCH($R5,$B$5:$B$36,0),MATCH(S$4,$C$4:$N$4,0))

copy across and down to AD16

Update: Hang on this will only work for the first group

Thinking...