In Columns D and E put this formula, starting with column 2:

D2:

=if(Search("D",A1)>0, if(C2<>"", A2,""), "")

E2:

=if(D2<>"", B2, "")

Now you should get values in columns D and E only in the places where you want to insert into columns A and B.

Turn on filters and filter D and E so they only show non-blanks.

Copy everything from D and E and paste into A and B.

(I haven't tested it yet, so make a backup first to ensure you can restore A and B as needed.)