Hi, David,
(a) Sally inquired who the man was whom I had been talking to.
(b) Sally inquired who the man was to whom I had been talking.
(c) Sally inquired who the man was I had been talking to.
(d) Sally inquired who the man was that I had been talking to.
(e) Sally inquired who the man whom I had been talking to was.
(f) Sally inquired who the man that I had been talking to was.
(g) Sally inquired who the man I had been talking to was.
When I saw those examples, it came to me at once that I discussed that particular question with you before. I thought there was something wrong with the model answer and took your opinion. The following is part of your e-mail (sent on January 3rd, 2019):
"I agree with your sense that there is something wrong with the model answer: "Sama inquired who was the man I had been talking to." The best answer is the one with the simple past and no inversion: "Sama inquired who the man was I had been talking to."
Assuming that the model answer isn't a typo, it's possible that the author(s) of the outside book thought that the order "was the man" was needed so that the zero relative clause ("I had been talking to") would immediately follow the noun it modifies ("man"). However, it's much better with extraposition, where "was" comes in between. Here are some other possibilities:
(a) Sama inquired who the man was who(m)/that I had been talking to.
(b) Sama inquired who the man was to whom I had been talking.
(c) Sama inquired who the man I had been talking to was.
(d) Sama inquired who the man who(m)/that I had been talking to was.
(e) Sama inquired who the man to whom I had been talking was.
Of those other versions, I like (a) the most and (e) the least. Sentence (c), strange as it may look, could easily occur in live speech. But all of them are grammatically correct. Of the four answers listed in your outside book, there is no question that "Sama inquired who the man was I had been talking to" is the best answer."
____________________________________________________________________________
I think our answer above (to Mr. Essam) is what is called 'automatic thoughts'.

I think the author of that question thought of two things:
1. The rule which says that if the original question begins 'what, which, or who' followed by be + complement, we can put the complement before or after 'be' in the report.
2. The necessity of using the zero relative clause.
Unfortunately, the author didn't think about the idea of 'extraposition', particularly it rarely appears in formal exams.