With a categories field, how do I query only the *selected* categories? Suppose we have a category group that allows for any number of levels to its structure. We also have a category field that allows for choosing any number of categories from that group.

In our example, we want to select three categories: one is a level 1 category in that group, another is a direct child of a different level 1 category and the third is a level 4 category from another branch in the category group (all three are in different branches for this example).

The question is: How do we query the category field to only return the categories we selected—the categories we actually clicked on when selecting categories? (Craft 3)
Here's how to isolate only your chosen categories. This works off the premise that if a related category is a parent of one of the other related categories, it is not a category you chose.
{% set relatedCategories = entry.myCategoryField.with(['parent']).all() %}
{% set parentCategories = relatedCategories | map(c =&gt; c.parent) %}
{% set chosenCategories = relatedCategories | diff(parentCategories) %}