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.

给定一个仅由字符 $\texttt{A}$、$\texttt{B}$ 和 $\texttt{C}$ 组成的字符串 $s$。

在一次操作中,你可以选择字符串中两个相邻的元素 $s_is_{i+1}$,前提是它们的顺序是以下之一:$\texttt{AB}$、$\texttt{BC}$ 或 $\texttt{CA}$,并交换它们。

请找出字符串 $s$ 上最多可以连续进行多少次这样的操作。

本题中,每组测试包含若干组输入数据。你需要分别独立地解决每组输入数据。

输入

第一行包含一个整数 $T$ $(1\le T\le 10^5)$~--- 表示输入数据的组数。接下来是每组输入数据的描述。

每组输入数据的第一行包含一个整数 $n$ $(1 \le n \le 10^6)$~--- 字符串 $s$ 的长度。

每组输入数据的第二行包含一个长度为 $n$ 的字符串 $s$,由字符 $\texttt{A}$、$\texttt{B}$ 和 $\texttt{C}$ 组成。

保证每组测试中所有输入数据的 $n$ 之和不超过 $10^6$。

输出

对于每组输入数据,在单独的一行输出一个整数~--- 字符串 $s$ 上最多可以连续进行的操作次数。

示例

输入

2
5
ABCCA
19
CCAABBBABBAAABBCCAA

输出

3
28

说明

在第一个样例的第一组输入中,字符串 $\texttt{ABCCA}$ 最多只能进行 $3$ 次连续操作。以下是一个能进行 $3$ 次操作的例子:[$\textbf{AB}\texttt{CCA}$ $\rightarrow$ $\texttt{BACCA}$, $\texttt{BAC}\textbf{CA}$ $\rightarrow$ $\texttt{BACAC}$, $\texttt{BA}\textbf{CA}\texttt{C}$ $\rightarrow$ $\texttt{BAACC}$]。

评分

设 $K$ 是一组测试中所有输入数据的 $n$ 之和。

  1. ($2$ 分):答案等于 $0$ 或 $1$;
  2. ($3$ 分):$n \le 3$;
  3. ($5$ 分):对所有 $1 \le i \le n$,有 $s_i \ne \texttt{C}$;
  4. ($5$ 分):$s$ 的形式为 $\texttt{AA}\ldots \texttt{AABB}\ldots \texttt{BBCC}\ldots \texttt{CC}$(即由若干个 $\texttt{A}$、若干个 $\texttt{B}$ 和若干个 $\texttt{C}$ 拼接而成);
  5. ($9$ 分):对所有 $1 \le i < n$,有 $s_is_{i+1} \ne \texttt{CA}$;
  6. ($10$ 分):$T = 1$ 且 $n \le 12$;
  7. ($8$ 分):$n \le 12$;
  8. ($28$ 分):$K \le 2000$;
  9. ($30$ 分):无额外限制。