For a simple answer, just
(1) remove \n
(2) use for-loop to print *
(3) print nextline
For a advance answer:
You have to think if the "a1" > "b2" and "a1" is large than the window size,
Can we prevent to print a1 more than one line?
You can think about that