The wireless card in your iMac, which is what is responsible for handling Wifi connections, is only capable of one connection at a time, to explain it most simply. The card cannot connect to a Wifi signal (your iPhone's Hotspot feature) and broadcast a signal at the same time.
However, a solution to what you wish to achieve may be possible if you can use USB Tethering as opposed to Wifi Hotspot on iPhone A. Then your wireless card on your iMac would be free to broadcast a Wifi signal for internet sharing.
I have not used any iPhone OS version since 3.1.2, where it was not called "hotspot" anywhere and the settings for tethering were even in a different set of menus. However, a quick search seems to show that you can activate USB tethering by going into your Personal Hotspot on the phone (Settings > General > Cellular > Personal Hotspot) while USB and Bluetooth are turned off. Then turning on Personal Hotspot, and tapping "USB Only" when prompted. USB tethering is also supposedly turned on even if Wifi and/or Bluetooth are on, yet I personally prefer to not have them on, and especially to not have the phone making a hotspot if I'm only going to use USB for connectivity.
Be advised, the iTunes Helper process is required to be running to provide the network interface for iPhone USB Tethering. This requires iTunes to be installed. If the iPhone does not initially provide you a network connection on your iMac, install if necessary and open iTunes, and it should start the iTunes Helper process and also add it to your login items.
At this point, your iMac should have a connection through your iPhone via USB. You can enable Wifi Internet Sharing in System Preferences and you should be good to go. Internet Sharing on most Macs, and I'm pretty certain on every modern one, produces a network in Infrastructure Mode. Which means there should be no problems connecting to it, even for devices that don't support certain other modes such as ad-hoc. So your other iPhone (B) should work fine.