Little Y is a college student who is currently doing researches related to strings. Little Y learned about the following definitions regarding strings:
- Given a string of length , we define its substring as the new string obtained by selecting in order and concatenating them.
- Given a string of length , we define its reversed result as the string obtained by concatenating in order, which is the string obtained by reversing the original string.
- Given two strings and of equal length , we define to be lexicographically smaller than if and only if there exists such that for any , and .
After understanding the above definitions, Little Y came up with the following problem:
Given a string of length , there are queries, each query giving two parameters and . You need to find out how many values of satisfy the following conditions:
- .
- is lexicographically smaller than .
Little Y would like to ask for your help in solving this problem.
Input Specification
This problem has multiple test data sets.
The first line of the input contains two integers and , which represent the test case number and the number of test data sets. represents that this test case is a sample test.
Then, each set of test data is given as input in order. For each set of test data:
The first line of input contains two positive integers, and , which represent the length of the string and the number of queries respectively.
The second line of input contains a string of length that only consists of lowercase letters.
Then lines follow, each containing two positive integers, and , representing a query. It is guaranteed that .
Output Specification
For each query of each set of test data, output a line containing an integer, representing the number of s satisfying the requirements.
Sample Input 1
0 2
9 3
abacababa
1 4
2 4
3 3
9 3
abaabaaba
1 4
2 4
3 3
Sample Output 1
4
0
3
2
0
2
Explanation for Sample Output 1
For the first set of test data in the sample:
- When , , .
- When , , .
- When , , .
- When , , .
In all four cases, is lexcicographically smaller than , so the answer is .
Additional Samples
Sample inputs and outputs can be found here.
- Sample 2 (
ex_string2.in
andex_string2.ans
) corresponds to test case 5. - Sample 3 (
ex_string3.in
andex_string3.ans
). - Sample 4 (
ex_string4.in
andex_string4.ans
) corresponds to test cases 24-25.
Constraints
For all test data, it is guaranteed that: . The string only consists of lowercase letters.
Test ID | Additional Constraints | ||
---|---|---|---|
A | |||
None | |||
A | |||
B | |||
None | |||
Additional Constraint A: It is guaranteed that the input string only consists of a
and b
, and each character is uniformly chosen from a
and b
at random.
Additional Constraint B: It is guaranteed that every pair of adjacent characters in the input string are distinct.
Comments