A gun is sited on a horizontal plain and can fire shells in any direction and at any elevation at speed $v$. The gun is a distance $d$ from a straight railway line which crosses the plain, where $v^2>gd$. The gunner aims to hit the line, choosing the direction and elevation so as to maximize the time of flight of the shell. Show that the time of flight, $T$, of the shell satisfies
$$
g^2 T^2 = 2 v^2 +2 \left(v^4 -g^2d^2\right)^{\frac12}\,.
$$