So you have B = A x T
therefore A-1 x B = T
So take the matrix inverse of A and multiply that by B to get the Transform matrix.
Then extract the quaternion, (or Euler angles, if you would prefer), using say http://www.cg.info.hiroshima-cu.ac.jp/~miyazaki/knowledge/teche52.html