Query which excludes tasks under set of namespaces

Something like this:

(not
  [?page :block/namespace ?ns]
  [?ns :block/name ?ns-name]
  [(contains? #{"name1" "name2"} ?ns-name)]
)

For multi-level namespaces, line [?page :block/namespace ?ns] can be replaced with a recursive rule, like in TODOs from parent and child pages