Population inversion by resonant magnetic confinement in terahertz quantum-cascade lasers