CF1997C.Even Positions

传统题 时间 2000 ms 内存 256 MiB 5 尝试 1 已通过 1 标签

Even Positions

Monocarp had a regular bracket sequence ss of length nn (nn is even). He even came up with his own way to calculate its cost.

He knows that in a regular bracket sequence (RBS), each opening bracket is paired up with the corresponding closing bracket. So he decided to calculate the cost of RBS as the sum of distances between pairs of corresponding bracket pairs.

For example, let's look at RBS (())(). It has three pairs of brackets:

  • (\_\_)\_\_: the distance between brackets at position 11 and at 44 is 41=34 - 1 = 3;
  • \_()\_\_\_: the distance is 32=13 - 2 = 1;
  • \_\_\_\_(): the distance is 65=16 - 5 = 1.

So the cost of (())() is 3+1+1=53 + 1 + 1 = 5.

Unfortunately, due to data corruption, Monocarp lost all characters on odd positions s1,s3,,sn1s_1, s_3, \dots, s_{n-1}. Only characters on even positions (s2,s4,,sns_2, s_4, \dots, s_{n}) remain. For example, (())() turned to \_(\_)\_).

Monocarp wants to restore his RBS by placing brackets on the odd positions. But since the restored RBS may not be unique, he wants to choose one with minimum cost. It's too hard to do for Monocarp alone, so can you help him?

Reminder: A regular bracket sequence is a string consisting of only brackets, such that this sequence, when inserted 1-s and +-s, gives a valid mathematical expression. For example, (), (()) or (()())() are RBS, while ), ()( or ())(() are not.

Input

The first line contains a single integer tt (1t50001 \le t \le 5000) — the number of test cases. Next tt cases follow.

The first line of each test case contains a single integer nn (2n21052 \le n \le 2 \cdot 10^5; nn is even) — the length of string ss.

The second line of each test case contains a string ss of length nn, where all characters on the odd positions are '\_' and all characters on the even positions are either '(' or ')'.

Additional constraints:

  • ss can be restored to at least one regular bracket sequence;
  • the total sum of nn over all test cases doesn't exceed 21052 \cdot 10^5.

Output

For each test case, print one integer — the minimum cost of the regular bracket sequence that can be obtained from ss by replacing '\_'-s with brackets.

Note

In the first test case, it's optimal to make ss equal to (())(). The cost of ss will be equal to 3+1+1=53 + 1 + 1 = 5.

In the second test case, the only option is to make ss equal to () with cost 11.

In the third test case, the only possible RBS is ()()()() with cost 1+1+1+1=41 + 1 + 1 + 1 = 4.

In the fourth test case, it's optimal to make ss equal to (())(()) with cost 3+1+3+1=83 + 1 + 3 + 1 = 8.

Samples

4
6
_(_)_)
2
_)
8
_)_)_)_)
8
_(_)_(_)
5
1
4
8

在线编程 IDE

建议全屏模式获得最佳体验