In your case, I would use
https://www.kamailio.org/docs/modules/5.6.x/modules/registrar#registrar.f.reg_fetch_contacts
See example:
https://www.kamailio.org/docs/modules/5.6.x/modules/registrar#idm871

You can iterate over it
Then for the first entry, you can set $ru and $du variables and it will become the main branch
For the other do append_branch and set parameters of the branch[-1] accordingly.
So you can control branch creation instead of automatically doing it by Kamailio and filter all needed.