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.
// package cf.cf1428;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
import java.util.function.IntConsumer;

public class CFC {
    private static final String INPUT = "3\n" +
            "AAA\n" +
            "BABA\n" +
            "AABBBABBBB\n";
    private static final int MOD = 1_000_000_007;

    FastScanner sc;
    PrintWriter out;

    public static void main(String[] args) {
        new CFC().run();
    }

    public CFC() {
        sc = new FastScanner(oj ? System.in : new ByteArrayInputStream(INPUT.getBytes()));
        out = new PrintWriter(System.out);
    }

    public void run() {
        long start = System.currentTimeMillis();
        times(sc.nextInt(), this::solve);
        out.flush();
        tr(System.currentTimeMillis() - start + "ms");
    }

    public void solve(int time) {
        List<Character> list = new LinkedList<>();
        for (char chr : sc.nextLine().toCharArray()) {
            list.add(chr);
        }

        int ans = list.size();
        for(ListIterator<Character> it = list.listIterator(); it.hasNext(); ) {
            Character chr = it.next();

            if (chr == 'B') {
                if (it.hasPrevious()) {
                    it.previous();
                    if (it.hasPrevious()) {
                        it.previous();
                        it.remove();
                        it.next();
                        it.remove();
                        ans -= 2;
                    } else {
                        it.next();
                    }
                }
            }
        }
        answer(ans);
    }

    //---------------------------------------------------------------------------------------------------

    /**
     * If searched element doesn't exist, returns index of first element which is bigger than searched value.<br>
     * If searched element is bigger than any array element function returns first index after last element.<br>
     * If searched element is lower than any array element function returns index of first element.<br>
     * If there are many values equals searched value function returns first occurrence.<br>
     */
    private static int lowerBound(long[] arr, long key) {
        int lo = 0;
        int hi = arr.length - 1;
        while (lo < hi) {
            int mid = (lo + hi) / 2;
            if (key <= arr[mid]) {
                hi = mid - 1;
            } else {
                lo = mid + 1;
            }
        }
        return arr[lo] < key ? lo + 1 : lo;
    }

    /**
     * Returns index of first element which is grater than searched value.
     * If searched element is bigger than any array element, returns first index after last element.
     */
    private static int upperBound(long[] arr, long key) {
        int lo = 0;
        int hi = arr.length - 1;
        while (lo < hi) {
            int mid = (lo + hi) / 2;
            if (key >= arr[mid]) {
                lo = mid + 1;
            } else {
                hi = mid;
            }
        }
        return arr[lo] <= key ? lo + 1 : lo;
    }

    private long[] prefixSum(long[] array) {
        long[] result = new long[array.length];
        result[0] = array[0];
        for (int i = 1; i < array.length; i++) result[i] = result[i - 1] + array[i];
        return result;
    }

    private int[] sort(int[] array) {
        shuffle(array);
        Arrays.sort(array);
        return array;
    }

    private static void shuffle(int[] array) {
        Random random = new Random();
        for (int i = array.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            {
                int tmp = array[index];
                array[index] = array[i];
                array[i] = tmp;
            }
        }
    }

    private static int gcd(int a, int b) {
        BigInteger b1 = BigInteger.valueOf(a);
        BigInteger b2 = BigInteger.valueOf(b);
        BigInteger gcd = b1.gcd(b2);
        return gcd.intValue();
    }

    private long[] sort(long[] array) {
        shuffle(array);
        Arrays.sort(array);
        return array;
    }

    private static void shuffle(long[] array) {
        Random random = new Random();
        for (int i = array.length - 1; i > 0; i--) {
            int index = random.nextInt(i + 1);
            {
                long tmp = array[index];
                array[index] = array[i];
                array[i] = tmp;
            }
        }
    }

    static class ExitException extends RuntimeException {
    }

    private int iread() {
        int res = sc.nextInt();
        if (res <= 0) {
            throw new ExitException();
        }
        return res;
    }

    private void iwrite(char[] chars) {
        for (char chr : chars) {
            out.print(chr);
        }
        out.println();
        out.flush();
    }

    private void write(Object obj) {
        out.print(obj);
    }

    private void writeln(Object obj) {
        out.println(obj);
    }

    private void writeln() {
        out.println();
    }

    private void answer(Object ans) {
        out.println(ans);
        throw new ExitException();
    }

    private void times(int n, IntConsumer consumer) {
        for (int i = 0; i < n; i++) {
            try {
                consumer.accept(i);
            } catch (ExitException ignored) {
            }
        }
    }

    private int[] readIntArray(int n) {
        int[] res = new int[n];
        for (int i = 0; i < n; i++) {
            res[i] = sc.nextInt();
        }
        return res;
    }

    private long[] readLongArray(int n) {
        long[] res = new long[n];
        for (int i = 0; i < n; i++) {
            res[i] = sc.nextLong();
        }
        return res;
    }

    static class FastScanner {
        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;

        FastScanner(InputStream stream) {
            this.stream = stream;
        }

        int read() {
            if (numChars == -1)
                throw new InputMismatchException();
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0) return -1;
            }
            return buf[curChar++];
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }

        boolean isEndline(int c) {
            return c == '\n' || c == '\r' || c == -1;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public double nextDouble() {
            return Double.parseDouble(next());
        }

        public String next() {
            int c = read();
            while (isSpaceChar(c)) c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public String nextLine() {
            int c = read();
            while (isEndline(c))
                c = read();
            StringBuilder res = new StringBuilder();
            do {
                res.appendCodePoint(c);
                c = read();
            } while (!isEndline(c));
            return res.toString();
        }

    }

    private boolean oj = System.getProperty("ONLINE_JUDGE") != null;

    private void tr(Object... o) {
        if (!oj) System.out.println(Arrays.deepToString(o));
    }
}
