Stability estimates in inverse problems for the Schrödinger and wave equations with trapping