Solve for A gA+QA=f