← Home
write a go solution for Description:
You are given a string s of length n consisting of characters textttA and textttB. You are allowed to do the following operation:

- Choose an index 1<=i<=n-1 such that s_i=textttA and s_i+1=textttB. Then, swap s_i and s_i+1.

You are only allowed to do the operation at most once for each index 1<=i<=n-1. However, you can do it in any order you want. Find the maximum number of operations that you can carry out.

Input Format:
Each test contains multiple test cases. The first line contains the number of test cases t (1<=t<=1000). Description of the test cases follows.

The first line of each test case contains a single integer n (2<=n<=2*10^5) — the length of string s.

The second line of each test case contains the string s (s_i=textttA or s_i=textttB).

It is guaranteed that the sum of n over all test cases does not exceed 2*10^5.

Output Format:
For each test case, print a single integer containing the maximum number of operations that you can carry out.

Note:
In the first test case, we can do the operation exactly once for i=1 as s_1=textttA and s_2=textttB.

In the second test case, it can be proven that it is not possible to do an operation.

In the third test case, we can do an operation on i=2 to form textttABAB, then another operation on i=3 to form textttABBA, and finally another operation on i=1 to form textttBABA. Note that even though at the end, s_2=textttA and s_3=textttB, we cannot do an operation on i=2 again as we can only do the operation at most once for each index.. Output only the code with no comments, explanation, or additional text.