If it is similar, the infringement is established. If B has been put into production, it is necessary to compensate A or use the patent.
If they are not similar, B can apply for a patent without infringing A's patent right.
This does not involve priority, and the application date of A is subject to the first application.
As you said, B's creation may be similar to A's. B's patent application was unsuccessful. If it is used, A can sue you for infringement. Continued use requires authorization from a.