D - ABC Transform Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MB

配点 : 400

問題文

A, B, C のみからなる文字列 S が与えられます。

S^{(0)}:=S とし、i=1,2,3,\ldots について S^{(i)}S^{(i-1)} の各文字を ABC, BCA, CAB と同時に置き換えたものと定義します。

以下の Q 個のクエリに答えてください。i 個目のクエリの内容は以下の通りです。

  • S^{(t_i)} の先頭から k_i 文字目を出力せよ。

制約

  • SA, B, C のみからなる長さ 1 以上 10^5 以下の文字列
  • 1 \leq Q \leq 10^5
  • 0 \leq t_i \leq 10^{18}
  • 1 \leq k_i \leq \min(10^{18},\ S^{(t_i)} の長さ)
  • Q, t_i, k_i は整数

入力

入力は以下の形式で標準入力から与えられる。

S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q

出力

Q 個のクエリを添字の昇順に、すなわち与えられる順に処理し、出力ごとに改行せよ。


入力例 1

ABC
4
0 1
1 1
1 3
1 6

出力例 1

A
B
C
B

S^{(0)}=ABC, S^{(1)}=BCCAAB です。

よって各クエリへの答えは順に A, B, C, B となります。


入力例 2

CBBAACCCCC
5
57530144230160008 659279164847814847
29622990657296329 861239705300265164
509705228051901259 994708708957785197
176678501072691541 655134104344481648
827291290937314275 407121144297426665

出力例 2

A
A
C
A
A

Score : 400 points

Problem Statement

You are given a string S consisting of A, B, C.

Let S^{(0)}:=S. For i=1,2,3,\ldots, let S^{(i)} be the result of simultaneously replacing the characters of S^{(i-1)} as follows: ABC, BCA, CAB.

Answer Q queries. The i-th query is as follows.

  • Print the k_i-th character from the beginning of S^{(t_i)}.

Constraints

  • S is a string of length between 1 and 10^5 (inclusive) consisting of A, B, C.
  • 1 \leq Q \leq 10^5
  • 0 \leq t_i \leq 10^{18}
  • 1 \leq k_i \leq \min(10^{18}, the length of S^{(t_i)})
  • Q, t_i, k_i are integers.

Input

Input is given from Standard Input in the following format:

S
Q
t_1 k_1
t_2 k_2
\hspace{0.4cm}\vdots
t_Q k_Q

Output

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


Sample Input 1

ABC
4
0 1
1 1
1 3
1 6

Sample Output 1

A
B
C
B

We have S^{(0)}=ABC, S^{(1)}=BCCAAB.

Thus, the answers to the queries are A, B, C, B in the given order.


Sample Input 2

CBBAACCCCC
5
57530144230160008 659279164847814847
29622990657296329 861239705300265164
509705228051901259 994708708957785197
176678501072691541 655134104344481648
827291290937314275 407121144297426665

Sample Output 2

A
A
C
A
A