Comments

An easier to think of solution for G is instead of doing the cyclic shift, simply take the first a columns with the smallest number of ones, and add ones to those in the next row. Code is here: https://codeforces.me/contest/1360/submission/81428756

It's a lot slower, but it still manages to fit within the time limit