How can I apply different templates based on taxonomy hierarchy?
What template exactly? taxonomy-term.html.twig, I guess?

You're looking for template suggestions. See Working with template suggestions on Drupal 8 for a similar question.

You can implement hook_template_suggestions_TEMPLATE() and then provide alternative file names for whatever condition you can think of.