contour integration – Evaluation of a Definite Integral in terms of the Beta function

On page 263 of A Course of Modern Analysis by Whittaker & Watson, one is asked to show that
$$int_0^{frac{pi }{2}} cos
^{p+q-2}(theta ) cos
((p-q) theta ) , dtheta
=frac{pi }{(p+q-1)
2^{p+q-1} B(p,q)}$$

(p+q<1) where B(p,q) is the Beta function.The suggested approach is to evaluate the contour integral of $z^{p-q-1}
left(z+frac{1}{z}right)^
{p+q-2}$
around a closed contour consisting of the straight line beween z=-i,i and the semicircle |z|=1 in the right half plane (indented at the branch point z=0,i,-i). I am having trouble evaluating the contribution from the two segments along the imaginary axis.They can be combined into the single integral
$$-i int_{-1}^1 (i y)^{p-q-1}
left(i
y-frac{i}{y}right)^{p+q-2
} , dy$$

Mathematica can evaluate this integral and returns the value
$$-frac{i sin (pi q) Gamma
(1-q) Gamma
(p+q-1)}{Gamma (p)}$$

which can easily be shown to be equivalent to
$$-frac{i pi }{(p+q-1) B(p,q)}$$
How do I derive this result ?