Gain dynamics in a heterogeneous terahertz quantum cascade laser