Trouble finding good letter writers for math grad school due to switching programs
I think X, Y and Z with both X and Y strong will be just fine. Few undergraduates have had enough contact with faculty to generate three strong letters.
The work you did with A and B  will be on your CV and you can discuss it in a cover letter.
Good luck.