Issue #25233: Rewrite the guts of Queue to be more understandable and correct. (Merge 3.4->3.5.)