From the sum-of-squares representation of a Boolean function to an optimal exact quantum query algorithm