#C5370. Longest Balanced Subsequence

    ID: 49012 Type: Default 1000ms 256MiB

Longest Balanced Subsequence

Longest Balanced Subsequence

Given a string S that consists of lowercase letters, your task is to determine the length of the longest balanced subsequence. A balanced subsequence is defined as a sequence which contains an equal number of 'a's and 'b's. Note that the characters in the subsequence need to appear in the same order as in the original string, but they are not required to be contiguous.

For instance, if S = "abba", the balanced subsequence can be "abba" itself, which contains two 'a's and two 'b's, with a total length of 4. Similarly, if S = "abcabc", the longest balanced subsequence containing only 'a's and 'b's is "abab" with a length of 4. If there are no pairs of 'a' and 'b', the answer should be 0.

You will be given T test cases. For each test case, output the length of the longest balanced subsequence.

inputFormat

The first line of input contains an integer T representing the number of test cases.

Each of the next T lines contains a string S, which may include lowercase letters.

outputFormat

For each test case, output a single integer denoting the length of the longest balanced subsequence. Each result should be printed on a new line.

## sample
3
abba
aabb
ababab
4

4 6

</p>