MG… This happens to all of us. Thoughts about things you may have seen/heard in the past… that weren’t good or wholesome… can “replay” in your head for a long time, after. That’s why it’s so important for us… to guard our eyes, ears and minds. Especially our eyes. They are the “lamps” of our soul… and will one day, behold God. I would encourage all… to be very careful about the movies or television shows you watch. There’s a lot of filth out there now.
But if a thought (such as you mentioned) does pass through your head, it is not sinful… of itself. It only becomes a sin, if somehow… we were to think of it… and then act on it, in a sinful way.
When an unpleasant thought occurs to me… I will usually say a “Glory Be to the Father”… sometimes, I say many of them… in rapid succession. And the enemy (being a coward) will usually stop the assault. Hope this helps. God bless.