Gauge invariance of $\theta$-term in QCD I have a problem with the $\theta$-term in the QCD-Lagrangian 
$$ \mathcal{L} = \overline{q}(x)i\gamma_\mu{D^\mu} q(x)-\overline{q}(x)\mathcal{M}q-\frac{1}{4}F^a_{\mu\nu}F_a^{\mu\nu}+ \theta \frac{g^2}{32 \pi^2} F_{\mu \nu}^a \tilde{F}^{\mu \nu}_a$$
I guess the last term is gauge invariante by itselfe since it is proportional to the trace: 
$$tr(F^{\mu\nu}\tilde{F}_{\mu\nu})=2\epsilon_{\mu\nu\rho\sigma}tr[(\partial^\mu A^\nu+igA^\mu A^\nu)(\partial^\rho A^\sigma +igA^\rho A^\sigma)]  $$
But on the other hand the $\theta$-term is topological non-trivial and the integral 
$$\int d^4x \ \ tr[F^{\mu\nu}\tilde{F}_{\mu\nu}] =: \int d^4x \ \ \partial_\mu K^\mu $$
is proportional to $n \in Z$ (the winding number) and not invariant under large gauge transformations.
Which step is wrong? And if the $\theta$-term is not gauge invariant how can we fix this problem in the Lagranian? 
 A: The problem is that $K^{\mu}$ is not a true vector on the manifold. It depends on the gauge potential $A$ as well as the gauge field $F$. It is alright to use the Stokes theorem for these types of objects locally on coordinate pathches but not globally on the whole manifold. 
Suppose that the integration is over $S^4$. We divide the integration into two parts, on the upper hemisphere and on the lower hemisphere, touching at the equator $S^3$.
We know that on each hemisphere (I am using a coordinate free notation)
$$\mathrm{tr}(F\wedge F) = d\mathrm{CS}(A)$$
Where $CS(A)$ is the Chern Simons class.
Using the Stokes theorem
$$\int_{S^4} \mathrm{tr}(F\wedge F)  = \int_{D_{up}^4} d\mathrm{CS}(A_{up})  + \int_{D_{down}^4} d\mathrm{CS}( A_{down}) = \int_{S^3} \mathrm{CS}( A_{up}) - \mathrm{CS}( A_{ down })$$
The local gauge potentials $ A_{up}$ and $ A_{down}$ are connected by a transit gauge transformation
$$A_{up} = gA_{down}g^{-1}+gdg^{-1}$$
It is known that the Chern-Simons classes are not gauge invariant, they change by a Wess-Zumino-Witten form under gauge transformation:
$$\mathrm{CS}( gA_{down}g^{-1}+ddg^{-1})- \mathrm{CS}( A_{ down })  = \mathrm{WZW}(g)$$
Thus we obtain:
$$\int_{S^4} \mathrm{tr}(F\wedge F)  = \int_{S^3}\mathrm{WZW}(g)$$
For nontrivial configurations like instantons, the transition gauge transformations are large gauge transformations, for which the Wess-Zumino-Witten is nonvanishing (It is the winding number of the transition group configurations).
