P18126 [ICPC 2026 Seoul R] Last Jump
Description
Two players Alice and Bob play the following game with a length-$n$ string $T=T[1]\circ T[2]\circ\cdots\circ T[n]$, where $T[i]$ belongs to the alphabet $\Sigma$ that consists of $26$ lowercase Latin letters, and $\circ$ is the concatenation operator.
1. The game starts with an empty string $X$ and an integer $i=0$, and Alice makes the first move.
2. The players alternate turns. On each turn, the current player chooses a character $c\in\Sigma$ and proceeds as follows.
1. If $T[i+1]=c$, then increment $i$ by $1$.
2. Otherwise, the player attempts a **jump** as follows.
1. Find the minimum $j$ such that $j>i$ and $T[j-m]\circ T[j-m+1]\circ\cdots\circ T[j]=X\circ c$, where $m$ is the length of $X$.
2. If no such $j$ exists, the game ends immediately and the current player loses.
3. Update $i$ to $j$.
3. In both cases, update $X$ to $X\circ c$.
3. If a successful move makes $i=n$, the game ends. The player who made the most recent jump wins. If no jump has occurred, Alice wins. Every successful move strictly increases $i$, so the game ends after at most $n$ successful moves.
The following table illustrates an example game with the string `abaababaaab`:
| Turn | $i$ | $X$ | Note |
| --- | --- | --- | --- |
| Start | $0$ | Empty string | |
| 1 (Alice) | $2$ | `b` | Alice chooses `b` (jump) |
| 2 (Bob) | $3$ | `ba` | Bob chooses `a` |
| 3 (Alice) | $4$ | `baa` | Alice chooses `a` |
| 4 (Bob) | $10$ | `baaa` | Bob chooses `a` (jump) |
| 5 (Alice) | $11$ | `baaab` | Alice chooses `b` (game ends, Bob wins) |
Bob wins this example game because he made the last jump, on Turn 4. However, note that if Alice had chosen `b` instead of `a` on Turn 3, Alice would have won. In fact, if both players play their best, the winner of the game with string `abaababaaab` is Alice.
Given $k$ strings, write a program to output the winner of the game for each of the $k$ strings when both players play their best.
Input Format
Your program is to read from standard input. The input starts with a line containing one integer $k$ ($1 \le k \le 10$) that indicates the number of strings.
Each of the next $k$ lines contains a string of $n$ lowercase Latin letters ($1 \le n \le 100{,}000$). The lengths of the strings may differ.
Output Format
Your program is to write to standard output. Print exactly one line for each string. Each line should contain exactly one capital Latin letter, either `A` if the winner is Alice or `B` if the winner is Bob, when the two players play their best with the corresponding string.