QOJ.ac

QOJ

実行時間制限: 1 s メモリ制限: 256 MB 満点: 100

#10459. Ali's Typewriter

統計

Ali likes to collect all sorts of strange things, and he recently acquired an old-fashioned typewriter. The typewriter has only 28 keys, labeled with the 26 lowercase English letters and the two characters 'B' and 'P'.

Ali discovered that the typewriter works as follows: Inputting a lowercase letter adds that letter to a groove in the typewriter (there is at least one letter in the groove before pressing 'P'). Pressing the key labeled 'B' removes the last letter from the groove. * Pressing the key labeled 'P' prints all the letters currently in the groove onto the paper and starts a new line, but the letters in the groove do not disappear (it is guaranteed that there is at least one letter in the groove).

For example, if Ali inputs aPaPBbP, the characters printed on the paper are as follows: a aa ab

We number the strings printed on the paper sequentially starting from 1 up to $n$. The typewriter has a very interesting feature: it contains a hidden numeric keypad. By inputting two numbers $(x, y)$ (where $1 \le x, y \le n$) on this keypad, the typewriter displays how many times the $x$-th printed string appears in the $y$-th printed string.

Ali was very excited after discovering this feature and wants to write a program to perform the same task. Can you help him?

Input

The first line contains a string, representing all the characters Ali inputs in order. The second line contains an integer $m$, representing the number of queries. The next $m$ lines describe all the queries entered via the numeric keypad. The $i$-th line contains two integers $x$ and $y$, representing the $i$-th query $(x, y)$.

Output

Output $m$ lines, where the $i$-th line contains an integer representing the answer to the $i$-th query.

Examples

Input 1

aPaPBbP
3
1 2
1 3
2 3

Output 1

2
1
0

Constraints

The range and characteristics of all test data are shown in the table below:

Test Case ID $n$ Scale $m$ Scale String Length Total Input Length (Number of characters in the first line of the input file)
1 $1 \le n \le 100$ $1 \le m \le 1000$ / $\le 100$
2
3 $1 \le n \le 1000$ $1 \le m \le 10^4$ Single length $\le 1000$, Total length $\le 10^5$ $\le 10^5$
4
5 $1 \le n \le 10^4$ $1 \le m \le 10^5$ Total length $\le 10^5$ $\le 10^5$
6
7
8 $1 \le n \le 10^5$ $1 \le m \le 10^5$ / $\le 10^5$
9
10

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.