Problem2223--abc242D - ABC Transform

2223: abc242D - ABC Transform

[Creator : ]
Time Limit : 1.000 sec  Memory Limit : 128 MB

Description

Time Limit: 2 sec / Memory Limit: 1024 MB

Score : 400400 points

Problem Statement

You are given a string SS consisting of A, B, C.
Let S^{(0)}:=SS(0):=S. For i=1,2,3,\ldotsi=1,2,3,, let S^{(i)}S(i) be the result of simultaneously replacing the characters of S^{(i-1)}S(i1) as follows: A → BC, B → CA, C → AB.
Answer QQ queries. The ii-th query is as follows.
  • Print the k_iki-th character from the beginning of S^{(t_i)}S(ti).

Constraints

  • SS is a string of length between 11 and 10^5105 (inclusive) consisting of A, B, C.
  • 1 \leq Q \leq 10^51Q105
  • 0 \leq t_i \leq 10^{18}0ti1018
  • 1 \leq k_i \leq \min(10^{18},1kimin(1018, the length of S^{(t_i)})S(ti))
  • Q, t_i, k_iQ,ti,ki are integers.

Input

Input is given from Standard Input in the following format:
SSQQt_1t1k_1k1t_2t2k_2k2\hspace{0.4cm}\vdotst_QtQk_QkQ

Output

Process the QQ queries in ascending order of index, that is, in the given order. Each answer should be followed by a newline.

Sample Input Copy

ABC
4
0 1
1 1
1 3
1 6

Sample Output Copy

A
B
C
B

HINT

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.

Source/Category