Niki Terpstra safely negotiated the final stage to win the Tour of Qatar as Arnaud Demare topped the final sprint.