Looking for closed-form least squares solution of a simplified inverse kinematics problem

Given $X_i, Y_i \in SO_3, i=1..n, n>2$, find $A,B$ that satisfies $AX_iB=Y_i$.

For $n=1$, there are an infinite number of solutions, a trivial one is:
$B=I$
$A=Y_1X_1^T$

For $n=2$, there should be exactly one solution. One could do the following:
Compute $A$ from the equation where $i=1$: $A=Y_1B^{-1}X_1^{-1}$,
Substituting this into equation where $i=2$: $Y_1B^{-1}X_1^{-1}X_2B=Y_2$,
Now, let $C=X_1^{-1}X_2$ and $D=Y_1^{-1}Y_2$,
We get: $B^{-1}CB=D$,
Which can be rewritten as: $CB=BD$
I'm already lost at this point. How do I solve for $B$, given $D,C$?

And now for $n>2$, how can I find a solution that minimizes the least squares error? Is it possible to rearrange the $n$ equations into a linear system at all? Or do I need to look into using some kind of iterative approach or linear programming instead?

-
$\mathbf{C}\mathbf{B}-\mathbf{B}\mathbf{D}=\mathbf{0}$ is a Sylvester equation, for which algorithms like Bartels-Stewart or Hessenberg-Schur apply.