I love AAAB
Let's call a string good if its length is at least $$$2$$$ and all of its characters are $$$\texttt{A}$$$ except for the last character which is $$$\texttt{B}$$$. The good strings are $$$\texttt{AB},\texttt{AAB},\texttt{AAAB},\ldots$$$. Note that $$$\texttt{B}$$$ is not a good string.You are given an initially empty string $$$s_1$$$.You can perform the following operation any number of times:   Choose any position of $$$s_1$$$ and insert some good string in that position. Given a string $$$s_2$$$, can we turn $$$s_1$$$ into $$$s_2$$$ after some number of operations?
In the first test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAB}} \to \texttt{A}\color{red}{\texttt{AB}}\texttt{AB}$$$.In the third test case, we transform $$$s_1$$$ as such: $$$\varnothing \to \color{red}{\texttt{AAAAAAAAB}}$$$.In the second and fourth test case, it can be shown that it is impossible to turn $$$s_1$$$ into $$$s_2$$$.
Each test contains multiple test cases. The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 10^4$$$) — the number of test cases. The description of the test cases follows.The first line of each test case contains a single string $$$s_2$$$ ($$$1 \leq |s_2| \leq 2 \cdot 10^5$$$).It is guaranteed that $$$s_2$$$ consists of only the characters $$$\texttt{A}$$$ and $$$\texttt{B}$$$.It is guaranteed that the sum of $$$|s_2|$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$.
For each test case, print "YES" (without quotes) if we can turn $$$s_1$$$ into $$$s_2$$$ after some number of operations, and "NO" (without quotes) otherwise.You can output "YES" and "NO" in any case (for example, strings "yEs", "yes" and "Yes" will be recognized as a positive response).
import sys

def solve():
    inp = sys.stdin.readline
    s2 = inp().strip()
    if s2[-1] != 'B':
        print('NO')
        return
    c = 0
    for i in s2:
        if i == 'A':
            c += 1
        else:
            if c == 0:
                print('NO')
                return
            c -= 1
    print('YES')

def main():
    for i in range(int(sys.stdin.readline())):
        solve()
if __name__ == '__main__':
    main()
n = int(input())
result = []
for i in range(n):
    count_a = 0
    count_b = 0
    a = input()
    l = len(a)
    if a[l - 1] != 'B' or a[0] != 'A':
        result.append('NO')
    else:
        flag = True
        for j in range(l):
            if a[j] == 'A':
                count_a += 1
            else:
                count_b += 1
            if count_a - count_b < 0:
                result.append('NO')
                flag = False
                break
        if flag:
            result.append('YES')
for i in result:
    print(i)
import math
NO = 'NO'
YES = 'YES'
MOD = 1000000007
ALPH = 'abcdefghijklmnopqrstuvwxyz'

def input_int():
    return int(input())

def input_list():
    return input().split(' ')

def input_list_int():
    return list(map(int, input_list()))

def list_int_to_str(a, sep=' '):
    return sep.join((str(x) for x in a))
global_ans = []

def pr(a):
    global_ans.append(a)

def z_func(s):
    n = len(s)
    z = [0] * n
    l = r = 0
    for i in range(1, n):
        if r >= i:
            z[i] = min(z[i - l], r - i + 1)
        while z[i] + i < n and s[z[i]] == s[z[i] + i]:
            z[i] += 1
        if i + z[i] - 1 > r:
            l = i
            r = i + z[i] - 1
    return z

def is_prime(n):
    d = 2
    while n % d != 0:
        d += 1
    return d == n

def partial_sum(a):
    b = [0]
    for i in a:
        b.append(b[-1] ^ i)
    return b

def partial_sum_2d(a):
    n = len(a)
    if n == 0:
        return []
    m = len(a[0])
    b = []
    for i in range(n + 1):
        b.append([0] * (m + 1))
    for i in range(n):
        for j in range(m):
            b[i + 1][j + 1] = a[i][j] + b[i][j + 1] + b[i + 1][j] - b[i][j]
    return b

def dfs(g, v, s, n):
    visited = [0] * (n + 1)

    def dfs_util(v, s):
        if v == s:
            return 1
        if visited[v]:
            return 0
        visited[v] = 1
        for i in g[v]:
            if not visited[i]:
                lr = dfs_util(i, s)
                if lr:
                    return 1
        return 0
    return dfs_util(v, s)

def solve():
    s = input()
    l = len(s)
    s = s[::-1]
    a = 0
    b = 0
    hasB = False
    for i in s:
        if i == 'B':
            b += 1
            hasB = True
        if i == 'A':
            if not hasB:
                pr(NO)
                return
            if b > 0:
                b -= 1
    if b > 0:
        pr(NO)
    else:
        pr(YES)
query_count = 1
query_count = input_int()
while query_count:
    query_count -= 1
    solve()
for global_ans_line in global_ans:
    if isinstance(global_ans_line, list):
        print(' '.join((str(x) for x in global_ans_line)))
    else:
        print(global_ans_line)