Monsters are magical creatures endowed with special powers, and they like to battle each other. Each Monster is of one of three types: Fire, Water or Grass. When two Monsters battle each other, one of them will win according to the following rules:
There are $$$N$$$ Monsters numbered $$$1 \ldots N$$$. You are given the type of each Monster $$$i$$$. You wish to perform $$$Q$$$ operations, where the $$$j$$$-th operation is the following:
Eventually, after some number of battles, there will only be one Monster remaining. You wish to find the number of Monster who could be the last remaining Monster. That is, you wish to count the number of Monster $$$i$$$ such that there is at least one valid sequence of battles and outcomes of battles by which $$$i$$$ is the last remaining Monster.
The first line contains a single integer $$$N$$$, the number of Monster.
The second line contains a string $$$A$$$ of $$$N$$$ characters without spaces, where the $$$i$$$-th character is F, W, or G, depending on whether the $$$i$$$-th Monster is Fire type, Water type, or Grass type, respectively.
The third line contains a single integer $$$Q$$$, the number of operations to be performed.
The next $$$Q$$$ lines describe these operations. The $$$j$$$-th of these lines contains two space-separated integers $$$L_j$$$ and $$$R_j$$$, which have the meaning explained above.
You should print $$$Q$$$ lines of output. The $$$j$$$-th of these lines should consist of a single integer, which is the number of Monster that could be the last remaining Monster in operation $$$j$$$.
The test data for this problem is divided into multiple subtasks. In order to pass a subtask, your submitted program must solve every test case within that subtask correctly and within the time and memory limits.
You will be awarded the points allocated to a subtask if at least one submission you make during the contest passes that subtask. You do not need to combine your solutions for multiple subtasks into a single submission.
Please keep in mind that the subtasks are not necessarily arranged in increasing order of difficulty. We encourage you to try as many subtasks as possible.
Constraints
In all test data, it is guaranteed that:
Subtasks
Constraint for Subtasks 1-9
You only have to perform one operation, and this operation covers all the Monster. In other words, $$$Q = 1$$$, $$$L_1=1$$$, $$$R_1=N$$$.
Constraint for Subtasks 10-11
There may be multiple operations.
7FFWGWFG21 22 4
2 2
5FWGFW11 5
4
35FWFWFWWWFWWFWWFFWFWWFWWWWFWFWWWFWWW11 35
23
Example 1
In this example, there are two operations.
Here is one possible sequence of battles by which Monster $$$2$$$ can be the last remaining Monster:
Here is one possible sequence of battles by which Monster $$$4$$$ can be the last remaining Monster:
There is no such sequence of battles by which Monster $$$3$$$ can be the last remaining Monster.
This example is valid for subtask 11.
Example 2
In this example, there is only one operation which covers all the Monster. It is possible for the Monster $$$1$$$, $$$2$$$, $$$3$$$ and $$$5$$$ to be the last remaining Monster, so the answer is $$$4$$$.
Here is one possible sequence of battles by which Monster $$$1$$$ is the last remaining Monster:
Similar to this, for each Monster $$$2$$$, $$$3$$$ or $$$5$$$ it is possible to find such a sequence of battles and outcomes of battles such that that Monster will be the last remaining Monster. For Monster $$$4$$$, it is impossible to find such a sequence.
This example is valid for subtasks 5, 6, 7, 8, 9 and 11.
Example 3
This example is valid for subtasks 2, 5, 6, 7, 8, 9, 10 and 11.