Let the function \( g \) be defined recursively as follows:
\[ g(n) = \begin{cases} 
n^2 + 2 & \text{if } n \text{ is prime} \\
g\left(\frac{n}{2}\right) + g\left(\frac{n}{3}\right) & \text{if } n \text{ is even and not prime} \\
g\left(\frac{n-1}{2}\right) + g\left(\frac{n+1}{2}\right) & \text{if } n \text{ is odd and not prime}
\end{cases} \]
Find \( g(10) \). Let's think step by step and output the final answer within \boxed{}.
user