Let $w_1$ and $w_2$ denote the [circles](https://artofproblemsolving.com/wiki/index.php/Circle) $x^2+y^2+10x-24y-87=0$ and $x^2 +y^2-10x-24y+153=0,$ respectively. Let $m$ be the smallest positive value of $a$ for which the line $y=ax$ contains the center of a circle that is externally [tangent](https://artofproblemsolving.com/wiki/index.php/Tangent_(geometry)) to $w_2$ and internally tangent to $w_1.$ Given that $m^2=\frac pq,$ where $p$ and $q$ are relatively prime integers, find $p+q.$
Rewrite the given equations as $(x+5)^2 + (y-12)^2 = 256$ and $(x-5)^2 + (y-12)^2 = 16$.
Let $w_3$ have center $(x,y)$ and radius $r$.  Now, if two circles with radii $r_1$ and $r_2$ are externally tangent, then the distance between their centers is $r_1 + r_2$, and if they are internally tangent, it is $|r_1 - r_2|$.  So we have
\begin{align*} r + 4 &= \sqrt{(x-5)^2 + (y-12)^2} \\ 16 - r &= \sqrt{(x+5)^2 + (y-12)^2} \end{align*}
Solving for $r$ in both equations and setting them equal, then simplifying, yields
\begin{align*} 20 - \sqrt{(x+5)^2 + (y-12)^2} &= \sqrt{(x-5)^2 + (y-12)^2} \\ 20+x &= 2\sqrt{(x+5)^2 + (y-12)^2} \end{align*}
Squaring again and canceling yields $1 = \frac{x^2}{100} + \frac{(y-12)^2}{75}.$
So the locus of points that can be the center of the circle with the desired properties is an [ellipse](https://artofproblemsolving.com/wiki/index.php/Ellipse).


Since the center lies on the line $y = ax$, we substitute for $y$ and expand: 
\[1 = \frac{x^2}{100} + \frac{(ax-12)^2}{75} \Longrightarrow (3+4a^2)x^2 - 96ax + 276 = 0.\]
We want the value of $a$ that makes the line $y=ax$ tangent to the ellipse, which will mean that for that choice of $a$ there is only one solution to the most recent equation. But a quadratic has one solution [iff](https://artofproblemsolving.com/wiki/index.php/Iff) its discriminant is $0$, so $(-96a)^2 - 4(3+4a^2)(276) = 0$.
Solving yields $a^2 = \frac{69}{100}$, so the answer is $\boxed{169}$.