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 java.util.*;
import java.io.*;

public class Main {

    static class Scan {

        private byte[] buf = new byte[1024];
        private int index;
        private InputStream in;
        private int total;

        public Scan() {
            in = System.in;
        }

        public int scan() throws IOException {
            if (total < 0) {
                throw new InputMismatchException();
            }
            if (index >= total) {
                index = 0;
                total = in.read(buf);
                if (total <= 0) {
                    return -1;
                }
            }
            return buf[index++];
        }

        public int scanInt() throws IOException {
            int integer = 0;
            int n = scan();
            while (isWhiteSpace(n)) {
                n = scan();
            }
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                if (n >= '0' && n <= '9') {
                    integer *= 10;
                    integer += n - '0';
                    n = scan();
                } else {
                    throw new InputMismatchException();
                }
            }
            return neg * integer;
        }

        public double scanDouble() throws IOException {
            double doub = 0;
            int n = scan();
            while (isWhiteSpace(n)) {
                n = scan();
            }
            int neg = 1;
            if (n == '-') {
                neg = -1;
                n = scan();
            }
            while (!isWhiteSpace(n) && n != '.') {
                if (n >= '0' && n <= '9') {
                    doub *= 10;
                    doub += n - '0';
                    n = scan();
                } else {
                    throw new InputMismatchException();
                }
            }
            if (n == '.') {
                n = scan();
                double temp = 1;
                while (!isWhiteSpace(n)) {
                    if (n >= '0' && n <= '9') {
                        temp /= 10;
                        doub += (n - '0') * temp;
                        n = scan();
                    } else {
                        throw new InputMismatchException();
                    }
                }
            }
            return doub * neg;
        }

        public String scanString() throws IOException {
            StringBuilder sb = new StringBuilder();
            int n = scan();
            while (isWhiteSpace(n)) {
                n = scan();
            }
            while (!isWhiteSpace(n)) {
                sb.append((char) n);
                n = scan();
            }
            return sb.toString();
        }

        private boolean isWhiteSpace(int n) {
            if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) {
                return true;
            }
            return false;
        }
    }

    public static void sort(int arr[], int l, int r) {    //sort(arr,0,n-1);
        if (l == r) {
            return;
        }
        int mid = (l + r) / 2;
        sort(arr, l, mid);
        sort(arr, mid + 1, r);
        merge(arr, l, mid, mid + 1, r);
    }

    public static void merge(int arr[], int l1, int r1, int l2, int r2) {
        int tmp[] = new int[r2 - l1 + 1];
        int indx1 = l1, indx2 = l2;
        //sorting the two halves using a tmp array
        for (int i = 0; i < tmp.length; i++) {
            if (indx1 > r1) {
                tmp[i] = arr[indx2];
                indx2++;
                continue;
            }
            if (indx2 > r2) {
                tmp[i] = arr[indx1];
                indx1++;
                continue;
            }
            if (arr[indx1] < arr[indx2]) {
                tmp[i] = arr[indx1];
                indx1++;
                continue;
            }
            tmp[i] = arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for (int i = 0, j = l1; i < tmp.length; i++, j++) {
            arr[j] = tmp[i];
        }
    }

    public static void sort(long arr[], int l, int r) {    //sort(arr,0,n-1);
        if (l == r) {
            return;
        }
        int mid = (l + r) / 2;
        sort(arr, l, mid);
        sort(arr, mid + 1, r);
        merge(arr, l, mid, mid + 1, r);
    }

    public static void merge(long arr[], int l1, int r1, int l2, int r2) {
        long tmp[] = new long[r2 - l1 + 1];
        int indx1 = l1, indx2 = l2;
        //sorting the two halves using a tmp array
        for (int i = 0; i < tmp.length; i++) {
            if (indx1 > r1) {
                tmp[i] = arr[indx2];
                indx2++;
                continue;
            }
            if (indx2 > r2) {
                tmp[i] = arr[indx1];
                indx1++;
                continue;
            }
            if (arr[indx1] < arr[indx2]) {
                tmp[i] = arr[indx1];
                indx1++;
                continue;
            }
            tmp[i] = arr[indx2];
            indx2++;
        }
        //Copying the elements of tmp into the main array
        for (int i = 0, j = l1; i < tmp.length; i++, j++) {
            arr[j] = tmp[i];
        }
    }

    public static void main(String args[]) throws IOException {
        Scan input = new Scan();
        StringBuilder ans = new StringBuilder("");
        int test = input.scanInt();
        for (int tt = 1; tt <= test; tt++) {
            String str = input.scanString();
            int lft[]=new int[str.length()];
            int rgt[]=new int[str.length()];
            boolean state[]=new boolean[str.length()];
            Arrays.fill(state, true);
            for(int i=0;i<str.length();i++) {
                lft[i]=i-1;
                rgt[i]=i+1;
            }
            for(int i=0;i<str.length();) {
//                System.out.println(i+" "+rgt[i]);
                if(rgt[i]<str.length() && str.charAt(i)=='A' && str.charAt(rgt[i])=='B') {
                    state[i]=state[rgt[i]]=false;
                    if(lft[i]>=0) {
                        rgt[lft[i]]=rgt[rgt[i]];
                    }
                    if(rgt[rgt[i]]<str.length()) {
                        lft[rgt[rgt[i]]]=lft[i];
                    }
                    if(lft[i]>=0) {
                        i=lft[i];
                    }
                    else {
                        i=rgt[rgt[i]];
                    }
                }
                else if(rgt[i]<str.length() && str.charAt(i)=='B' && str.charAt(rgt[i])=='B') {
                    state[i]=state[rgt[i]]=false;
                    if(lft[i]>=0) {
                        rgt[lft[i]]=rgt[rgt[i]];
                    }
                    if(rgt[rgt[i]]<str.length()) {
                        lft[rgt[rgt[i]]]=lft[i];
                    }
                    if(lft[i]>=0) {
                        i=lft[i];
                    }
                    else {
                        i=rgt[rgt[i]];
                    }
                }
                else {
                    i=rgt[i];
                }
            }
            int cnt=0;
            for(int i=0;i<str.length();i++) {
                if(state[i]) {
                    cnt++;
                }
            }
            ans.append(cnt+"\n");
        }
        System.out.println(ans);
    }
}
