Higher-Order Queries And Applications