Maliks Muwatta Book 4, Hadith Number 3.

Section : What to Do if One Forgets in Prayer.

 Yahya related to me from Malik that he had heard that a man questioned al-Qasim ibn Muhammad saying, “My imagination works in the prayer, and it happens to me a lot.” Al-Qasim ibn Muhammad said, “Go on with your prayer, for it will not go away from you until you go away saying, ‘I have not completed my prayer.'”