his coverage period attributable to the agreement with the State under this section shall end on the last day of whichever of the following first occurs:
(A)#
the month in which he is determined by the State agency to have become ineligible both for money payments of a kind specified in the agreement and (if there is in effect a modification entered into under subsection (h)) for medical assistance, or
(B)#
the month preceding the first month for which he becomes entitled to monthly benefits under subchapter II or to an annuity or pension under the Railroad Retirement Act of 1974 [ 45 U.S.C. 231 et seq.].