A Generalized Adversary Method for Quantum Query Complexity