If I pray behind an imam who is praying the Maghrib and I do not think to myself, 'I intend to pray the Maghrib behind this imam', so I merely intended to pray the Maghrib and joined the imam in prayer, does that affect my prayer?
All perfect praise be to Allah, The Lord of the worlds. I testify that there is none worthy of worship except Allah and that Muhammad is His slave and Messenger.
If you intended to pray behind the imaam who is leading the Maghrib prayer, then this is enough and your prayer is valid, and the fact that you did not designate the imaam (in your intention) does not harm; rather, the intention of praying behind the present imaam is sufficient.
The Kuwaiti Fiqh Encyclopedia reads, “The scholars of Fiqh agreed that the person led in prayer must intend to follow the imaam, but he is not obliged to designate him.”
Allah knows best.
You can search for fatwa through many choices