5. Form an $n$-digit number using the digits $1, 2, 3$, requiring that each of $1, 2, 3$ appears at least once in the $n$-digit number. The total number of such $n$-digit numbers is $\qquad$ .
5. $3^{n}-3 \cdot 2^{n}+3$.

Let the set of all $n$-digit numbers composed of the digits $1, 2, 3$ be denoted as $I$, and the sets of all $n$-digit numbers in $I$ that do not contain the digits $1, 2, 3$ be denoted as $A_{1}, A_{2}, A_{3}$, respectively. Then $|I|=3^{n},\left|A_{1}\right|=\left|A_{2}\right|=\left|A_{3}\right|=2^{n},\left|A_{i} \cap A_{j}\right|=1$, $(1 \leqslant i<j \leqslant 3),\left|A_{1} \cap A_{2} \cap A_{3}\right|=0$, thus $\left|\overline{A_{1} \cup A_{2} \cup A_{3}}\right|=|I|-\left|A_{1}\right|-\left|A_{2}\right|-\left|A_{3}\right|+$ $\left|A_{1} \cap A_{2}\right|+\left|A_{2} \cap A_{3}\right|+\left|A_{3} \cap A_{1}\right|-\left|A_{1} \cap A_{2} \cap A_{3}\right|=3^{n}-3 \cdot 2^{n}+3 \times 1-0=3^{n}-$ $3 \cdot 2^{n}+3$.