OPSMN-1587: Fixed logic in query engine to find first common ancestor of 2 different forms