I think the correct formula for q should be
q = ad + bc