Deriving the Bellman equation for value and Q functions