Even the dullest 1L can tell you that rule 12(b) sets the timing for motions to dismiss:
A motion asserting any of [the 12(b)] defenses must be made before pleading if a responsive pleading is allowed.
However, pursuant to 12(h), the defenses listed in 12(b)(2)-12(b)(5) are not actually waived as long as you include them as an affirmative defense in your answer (12(b)(1) and (b)(6) have separate rules).
A party waives any defense listed in Rule 12(b)(2)–(5) by . . failing to either:(i) make it by motion under this rule; or include it in a responsive pleading or in an amendment allowed by Rule 15(a)(1) as a matter of course
So then, if these defenses aren't waived, when can you …