QOJ.ac

QOJ

Time Limit: 2 s Memory Limit: 256 MB Total points: 100

#11567. Reversal ABC

统计

Given a string $s$ consisting of characters $\texttt{A}$, $\texttt{B}$, and $\texttt{C}$.

In one operation, you are allowed to choose two adjacent elements of the string $s_is_{i+1}$ that are in the following order: $\texttt{AB}$, $\texttt{BC}$, or $\texttt{CA}$, and swap them.

Find the maximum number of consecutive operations that can be performed on the string $s$.

In this problem, each test contains several sets of input data. You need to solve the problem independently for each such set.

Input

The first line contains one integer $T$ $(1\le T\le 10^5)$~--- the number of sets of input data. The description of the input data sets follows.

In the first line of each input data set, there is one integer $n$ $(1 \le n \le 10^6)$~--- the length of the string $s$.

In the second line of each input data set, there is a string $s$ of length $n$, consisting of characters $\texttt{A}$, $\texttt{B}$, and $\texttt{C}$.

It is guaranteed that the sum of $n$ across all input data sets of a single test does not exceed $10^6$.

Output

For each set of input data, output on a separate line one integer~--- the maximum number of consecutive operations that can be performed on the string $s$.

Example

Input

2
5
ABCCA
19
CCAABBBABBAAABBCCAA

Output

3
28

Notes

In the first set of input data from the first example, no more than $3$ consecutive operations can be performed on the string $\texttt{ABCCA}$. One example of how $3$ consecutive operations can be performed is [$\textbf{AB}\texttt{CCA}$ $\rightarrow$ $\texttt{BACCA}$, $\texttt{BAC}\textbf{CA}$ $\rightarrow$ $\texttt{BACAC}$, $\texttt{BA}\textbf{CA}\texttt{C}$ $\rightarrow$ $\texttt{BAACC}$].

Scoring

Let $K$ be the sum of $n$ over all input data sets of one test.

  1. ($2$ points): the answer is equal to $0$ or $1$;
  2. ($3$ points): $n \le 3$;
  3. ($5$ points): $s_i \ne \texttt{C}$ for $1 \le i \le n$;
  4. ($5$ points): $s$ has the form $\texttt{AA}\ldots \texttt{AABB}\ldots \texttt{BBCC}\ldots \texttt{CC}$ (that is, $x \cdot \texttt{A} + y \cdot \texttt{B} + z \cdot \texttt{C}$ for certain positive $x$, $y$, $z$);
  5. ($9$ points): $s_is_{i+1} \ne \texttt{CA}$ for $1 \le i < n$;
  6. ($10$ points): $T = 1$, $n \le 12$;
  7. ($8$ points): $n \le 12$;
  8. ($28$ points): $K \le 2000$;
  9. ($30$ points): without additional constraints.
About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.