#P1473D. Program

    ID: 2598 Type: RemoteJudge 2000ms 256MiB Tried: 0 Accepted: 0 Difficulty: 5 Uploaded By: Tags>data structuresdpimplementationstrings*1700

Program

Description

You are given a program that consists of nn instructions. Initially a single variable xx is assigned to 00. Afterwards, the instructions are of two types:

  • increase xx by 11;
  • decrease xx by 11.

You are given mm queries of the following format:

  • query ll rr — how many distinct values is xx assigned to if all the instructions between the ll-th one and the rr-th one inclusive are ignored and the rest are executed without changing the order?

The first line contains a single integer tt (1t10001 \le t \le 1000) — the number of testcases.

Then the description of tt testcases follows.

The first line of each testcase contains two integers nn and mm (1n,m21051 \le n, m \le 2 \cdot 10^5) — the number of instructions in the program and the number of queries.

The second line of each testcase contains a program — a string of nn characters: each character is either '+' or '-' — increment and decrement instruction, respectively.

Each of the next mm lines contains two integers ll and rr (1lrn1 \le l \le r \le n) — the description of the query.

The sum of nn over all testcases doesn't exceed 21052 \cdot 10^5. The sum of mm over all testcases doesn't exceed 21052 \cdot 10^5.

For each testcase print mm integers — for each query ll, rr print the number of distinct values variable xx is assigned to if all the instructions between the ll-th one and the rr-th one inclusive are ignored and the rest are executed without changing the order.

Input

The first line contains a single integer tt (1t10001 \le t \le 1000) — the number of testcases.

Then the description of tt testcases follows.

The first line of each testcase contains two integers nn and mm (1n,m21051 \le n, m \le 2 \cdot 10^5) — the number of instructions in the program and the number of queries.

The second line of each testcase contains a program — a string of nn characters: each character is either '+' or '-' — increment and decrement instruction, respectively.

Each of the next mm lines contains two integers ll and rr (1lrn1 \le l \le r \le n) — the description of the query.

The sum of nn over all testcases doesn't exceed 21052 \cdot 10^5. The sum of mm over all testcases doesn't exceed 21052 \cdot 10^5.

Output

For each testcase print mm integers — for each query ll, rr print the number of distinct values variable xx is assigned to if all the instructions between the ll-th one and the rr-th one inclusive are ignored and the rest are executed without changing the order.

Sample Input 1

2
8 4
-+--+--+
1 8
2 8
2 5
1 1
4 10
+-++
1 1
1 2
2 2
1 3
2 3
3 3
1 4
2 4
3 4
4 4

Sample Output 1

1
2
4
4
3
3
4
2
3
2
1
2
2
2

Note

The instructions that remain for each query of the first testcase are:

  1. empty program — xx was only equal to 00;
  2. "-" — xx had values 00 and 1-1;
  3. "---+" — xx had values 00, 1-1, 2-2, 3-3, 2-2 — there are 44 distinct values among them;
  4. "+--+--+" — the distinct values are 11, 00, 1-1, 2-2.