There is nothing wrong with saying "the" twice. In general, it is good (but not necessary) to avoid using the same word twice in a sentence. However, for this purpose I would not count "the" as a word.
None of these eight sentences is wrong. A context could be invented for each one but #1 and #5 are the safest. It would take some skill to invent contexts for #2, 3, 4, 6, 7, or 8, but it is possible.