Alternate entries based on lookup

This is more a ‘fun’ question than a serious one, because I can just do this as a macro. But it seems like a good ‘academic’ question.

I have a source of names and genders like this:
image

Then I have a table of these same names in teams that are optimized for gender distribution–same number of M’s as F’s per team, as is possible. Say the first two rows/teams are:

image

Now, I’d like to replicate this table, but have the names (as much as possible) alternate F-M-F-M…
So the example rows would become:

image

Any ideas? I get that there are lots of oddities, such as more M’s or F’s, etc. It would be fine to always start on F’s then just trail out with whatever is left.

AlternatingMF.ods (10.8 KB)

Spreadsheets do not deal with row sets. Databases do.
Open the report in 83334.odb (7.3 KB)