Time Limit: 2 sec / Memory Limit: 1024 MB
SSQQt_1t1k_1k1t_2t2k_2k2\hspace{0.4cm}\vdots⋮t_QtQk_QkQ
ABC
4
0 1
1 1
1 3
1 6
A
B
C
B
We have S^{(0)}=S(0)=ABC, S^{(1)}=S(1)=BCCAAB.
Thus, the answers to the queries are A, B, C, B in the given order.