#P17576. [JAG 2026 Summer Camp #3] Box Tower II
[JAG 2026 Summer Camp #3] Box Tower II
Problem Description
There are cube-shaped boxes of the same size, numbered . Each face of each box is colored either black or white.
The colors of the six faces of the -th box are denoted by , and , as shown in Figure E-1. Here, each of , and is either B or W, where B represents black and W represents white.

Figure E-1: Correspondence between the input and the faces of a box.
You are given queries. For each (), the -th query specifies two integers and .
Using all the boxes numbered , you would like to make a single tower of boxes by rotating each box and then stacking the boxes vertically. After the orientation of each box is determined, all boxes are placed so that their top and bottom faces are horizontal and their bases are exactly aligned. Thus, each of the four sides of the tower consists of exactly one face from each box.
A tower is called balanced if, on each of its four sides, the number of black faces is equal to the number of white faces.
For each (), determine whether it is possible to make a balanced tower using all the boxes numbered .
Input Format
The input consists of a single test case of the following format.
N Q
u_1d_1f_1b_1l_1r_1
u_2d_2f_2b_2l_2r_2
...
u_Nd_Nf_Nb_Nl_Nr_N
s_1 t_1
s_2 t_2
...
s_Q t_Q
The first line contains two integers and (, ), representing the number of boxes and the number of queries, respectively.
For each (), the -th of the following lines contains the six characters , and , in this order and without spaces. Each character is either B or W and represents the color of the corresponding face of the -th box as shown in Figure E-1.
For each (), the -th of the following lines contains two integers and (), representing the range of boxes used in the -th query.
Output Format
Print lines.
For each (), print Yes on the -th line if it is possible to make a balanced tower using all the boxes numbered , and print No otherwise.
4 5
BBWWBW
WWWBBB
BWBBWW
BBBBBB
1 2
2 3
3 4
1 4
2 2
Yes
Yes
No
Yes
No