12. Simplify: $\sum_{k=1}^{n} \frac{\cos 3^{k} x+3 \cos 3^{k-1} x}{3^{k-1} \sin 3^{k} x}$.
12. Since $\frac{2(\cos 3 \alpha+3 \cos \alpha)}{\sin 3 \alpha}=\frac{3(2 \cos \alpha+\cos 3 \alpha)-\cos 3 \alpha}{\sin 3 \alpha}=\frac{3\left(4 \cos ^{2} \alpha-1\right) \cos \alpha}{\sin \alpha\left(3-4 \sin ^{2} \alpha\right)}-\cot 3 \alpha=3 \cot \alpha-$ $\cot 3 \alpha$.

Let $a=3^{k-1} x, k=1,2, \cdots, n-1$, then
$$
\begin{array}{l}
\frac{\cos 3^{k} x+3 \cos 3^{k-1} x}{3^{k-1} \sin 3^{k} x}=\left(\frac{\cot 3^{k-1} x}{3^{k-2}}-\frac{\cot 3^{k} x}{3^{k-1}}\right) \cdot \frac{1}{2} \\
\sum_{k=1}^{n} \frac{\cos 3^{k} x+3 \cos 3^{k-1} x}{3^{k-1} \sin 3^{k} x}=\frac{1}{2} \sum_{k=1}^{n}\left(\frac{\cot 3^{k-1} x}{3^{k-2}}-\frac{\cot 3^{k} x}{3^{k-1}}\right)=\frac{1}{2}\left(3 \cot x-\frac{\cot 3^{n} x}{3^{n-1}}\right) .
\end{array}
$$