Why the same code, when submitted using GNU C++17 got accepted but when submitted using GNU C++17 (64) gave wrong answer ? This is the accepted one https://codeforces.net/contest/1365/submission/122433105 and this is the wrong one https://codeforces.net/contest/1365/submission/122431881 .
You have UB somewhere
more like compiler bug...
I tried your code in customtest with this test
1
2 2
#B
G.
added some print before final two loops
it prints
G
0
Yes
Removing one of
break;
(or both) gives correct behavior