As VALJ said, it's WHERE the foul took place that matters.
Virtually all fouls in FED are governed by ABO. The "one" in ABO are fouls by the offense behind the basic spot. In a), the basic spot is the 20. In b), the basic spot is the B10. Where did the foul take place? If it occurred behind the basic spot, the enforcement is from the spot of the foul. If the foul occurred in front of the basic spot, the enforcement is from the basic spot.
If the spot of the foul is in the end zone, it's a safety. If the spot of the foul is outside the end zone, it's not a safety, see above for enforcement.
Knowing your spots and ABO will almost always get you in the right place.
ADDED IN EDIT: I was typing while Ralph was posting. And as to the trivia question, no, B's foul occurred before the last change of possession, so it would not carry over. A would have to decline the foul to take the TD.