Output taxonomy term in views unformatted using twig
You're a little light on details about your setup, but I can make a suggestion based on what I see. I don't know how you're settings the term variable, but row will have access to the node (assuming this is a node view) that represents the row being displayed. You can do something similar to

{{ row.content['#node'].field_tags.getValue().0.target_id }}

(assuming field_tags is your term reference field) to print the referenced term ID.

Edit: just realized how long ago this was asked... Not sure why it was showing at the top of my list. Anyhow, hopefully helpful to someone!