AlgoUniversity
  • Go Back
Discussion
Substrings In Substring :

Author

Ayush Gangwani

Difficulty Level : Medium

Submissions : 254

Asked In : Sprinklr

Marks :15

: 6 | : 0

You are given a $$$1$$$-indexed string $$$S$$$ of length $$$n$$$ having only lowercase Latin letters. We call a substring that starts and ends with the same character as a good substring. Note that a single character is also considered as a good substring.

You need to answer $$$q$$$ queries of the following type:

  • $$$l$$$ $$$r$$$ — Find the number of good substrings of the substring $$$S[l...r]$$$

More formally, for each query, you need to print the number of substrings of the string $$$S[l...r]$$$ which start and end with the same character.

Input

The first line of each testcase contains an integer $$$t$$$ $$$(1 \le t \le 10^4)$$$ — the number of testcases. The description of $$$t$$$ testcases follows.

The first line of each testcase contains two space separated integers $$$n$$$ and $$$q$$$ — the length of the string $$$s$$$ and the number of queries respectively.

The second line of each testcase contains string $$$s$$$ containing $$$n$$$ lowercase Latin letters.

The next $$$q$$$ lines each contain two space separated integers $$$l$$$ and $$$r$$$ denoting the queries.

It is guaranteed that the individual sums of $$$n$$$ and $$$q$$$ over all testcases does not exceed $$$10^5$$$ respectively.

Output

For each testcase, print the answer to each query in a single line.

Example

Input
2
4 2
aabc
1 2
3 4
6 4
abcaab
1 1
2 5
3 6
1 6
Output
3
2
1
5
5
10

Note

For sample test case 1,

  • For the $$$1^{st}$$$ query, $$$l=1$$$, $$$r=2$$$. We have $$$3$$$ substrings that satisfy the above condition, $$$S[1,1]$$$, $$$S[1,2]$$$ and $$$S[2,2]$$$.
  • For the $$$2^{nd}$$$ query, $$$l=3$$$, $$$r=4$$$. We have $$$2$$$ substrings that satisfy the above condition, $$$S[3,3]$$$, $$$S[4,4]$$$.

You need to login to view your submissions.

You need to login to view all submissions.

Loading...

Result : Executed

Loading...

Feel something is wrong with the test cases?

Result : Accepted

Test Cases :

You need to Log In
We're glad that you want to attempt this problem!

But to Run or Submit the Problem, you need to Log In.

Continue to Log In
Challenge Submitted!

Your challenge has been submitted successfully.

You will get a response soon via WhatsApp or Email.

Challenge
Facing issue while trying to solve the problem! Don't worry, we got you covered!

Do let us know your issue.

Looks good!
Please enter your issue / feedback.

How do we get in touch with you?
Looks good!
Please enter your phone no.
Looks good!
Please enter your email address.