Efficient Datalog Rewriting for Query Answering in TGD Ontologies