Based on @Michal answer, the formulae to use for the example are:
=SUMPRODUCT((C2:C9=MINIFS(C2:C9,A2:A9,A2:A9))*(D2:D9="A"))
=SUMPRODUCT((C2:C9=MINIFS(C2:C9,A2:A9,A2:A9))*(D2:D9="B"))