Spin ensembles with a comb-shaped spectrum have shown exciting properties as efficient quantum memories. Here, we present a rigorous theoretical study of such atomic frequency combs in the strong coupling limit of cavity QED, based on a full quantum treatment using tensor-network methods. Our results demonstrate that arbitrary multiphoton states in the cavity are almost perfectly absorbed by the spin ensemble and reemitted as parity-flipped states at periodic time intervals. Fidelity values near unity are achieved in these revived states by compensating for energy shifts induced by the strong spin-cavity coupling through adjustments of individual coupling values of the teeth in the atomic frequency comb.