Zookeeper is playing a game. In this game, Zookeeper must use bombs to bomb a string that consists of letters 'A' and 'B'. He can use bombs to bomb a substring which is either "AB" or "BB". When he bombs such a substring, the substring gets deleted from the string and the remaining parts of the string get concatenated.

For example, Zookeeper can use two such operations: AABABBA → AABBA → AAA.

Zookeeper wonders what the shortest string he can make is. Can you help him find the length of the shortest string?

Input

Each test contains multiple test cases. The first line contains a single integer t (1 ≤ t ≤ 20000) — the number of test cases. The description of the test cases follows.

Each of the next t lines contains a single test case each, consisting of a non-empty string s: the string that Zookeeper needs to bomb. It is guaranteed that all symbols of s are either 'A' or 'B'.

It is guaranteed that the sum of |s| (length of s) among all test cases does not exceed 2 ⋅ 10^5.

Output

For each test case, print a single integer: the length of the shortest string that Zookeeper can make.

Example

Input


3
AAA
BABA
AABBBABBBB


Output


3
2
0

Note

For the first test case, you can't make any moves, so the answer is 3.

For the second test case, one optimal sequence of moves is BABA → BA. So, the answer is 2.

For the third test case, one optimal sequence of moves is AABBBABBBB → AABBBABB → AABBBB → ABBB → AB → (empty string). So, the answer is 0.
import os, sys
from io import IOBase, BytesIO
py2 = round(0.5)
if py2:
    from future_builtins import ascii, filter, hex, map, oct, zip
    range = xrange
BUFSIZE = 8192
class FastIO(BytesIO):
    newlines = 0
 
    def __init__(self, file):
        self._file = file
        self._fd = file.fileno()
        self.writable = 'x' in file.mode or 'w' in file.mode
        self.write = super(FastIO, self).write if self.writable else None
 
    def _fill(self):
        s = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
        self.seek((self.tell(), self.seek(0,2), super(FastIO, self).write(s))[0])
        return s
 
    def read(self):
        while self._fill(): pass
        return super(FastIO,self).read()
 
    def readline(self):
        while self.newlines == 0:
            s = self._fill(); self.newlines = s.count(b'\n') + (not s)
        self.newlines -= 1
        return super(FastIO, self).readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.getvalue())
            self.truncate(0), self.seek(0)
 
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        if py2:
            self.write = self.buffer.write
            self.read = self.buffer.read
            self.readline = self.buffer.readline
        else:
            self.write = lambda s:self.buffer.write(s.encode('ascii'))
            self.read = lambda:self.buffer.read().decode('ascii')
            self.readline = lambda:self.buffer.readline().decode('ascii')
 
 
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip('\r\n')
 
# Cout implemented in Python
import sys
class ostream:
    def __lshift__(self,a):
        sys.stdout.write(str(a))
        return self
cout = ostream()
endl = '\n'

def get_input(a=str):
    return a(input())

def get_int_input():
    return get_input(int)

def get_input_arr(a):
    return list(map(a, input().split()))

def get_int_input_arr():
    return get_input_arr(int)





def solve():
    
    t = get_int_input()

    for _ in range(t):

        st = get_input()
        mark = [True] * len(st)

        ptr_1 = 0
        ptr_2 = 0

        def find_next():
            nonlocal ptr_1, ptr_2
            while ptr_1 < len(st):
                if mark[ptr_1] and st[ptr_1] == "A":
                    break
                ptr_1 += 1
            
            while ptr_2 < len(st):
                if mark[ptr_2] and st[ptr_2] == "B" and ptr_2 > ptr_1:
                    break
                ptr_2 += 1

        find_next()
        while ptr_1 < len(st) and ptr_2 < len(st) and st[ptr_1] == "A" and st[ptr_2] == "B":
            mark[ptr_1] = False
            mark[ptr_2] = False

            # print(ptr_1, ptr_2)
            ptr_1 += 1
            ptr_2 += 1

            find_next()

        ptr_1 = 0
        ptr_2 = 0
        def find_next_2():
            nonlocal ptr_1, ptr_2
            while ptr_1 < len(st):
                if mark[ptr_1] and st[ptr_1] == "B":
                    break
                ptr_1 += 1

            while ptr_2 < len(st):
                if st[ptr_2] == "B" and mark[ptr_2] and ptr_2 > ptr_1:
                    break
                ptr_2 += 1

        # print(mark)
        find_next_2()
        while ptr_1 < len(st) and ptr_2 < len(st) and st[ptr_1] == "B" and st[ptr_2] == "B":
            mark[ptr_1] = False
            mark[ptr_2] = False

            # print(ptr_1, ptr_2)
            ptr_1 += 1
            ptr_2 += 1
            find_next_2()

        res = 0
        for i in range(len(mark)):
            if mark[i]:
                res += 1

        cout<<res<<endl



def main():
    solve()


if __name__ == "__main__":
    main()