Alice and Bob are taking a walk in the Land Of Doors which is a magical place having a series of N adjacent doors that are either open or close.
After a while they get bored and decide to do something interesting. So they started closing the open doors taking turns.
In each turn, the person walks upto any closed door.
He then selects a direction, either left or right, and starts to close the consecutive open doors in that direction.
He can stop whenever he wants or if he encounters a closed door.
However he must be able to close atleast one door. If he is unable to do so, he must concede defeat.
Your task is, given the initial state of doors in the Land Of Doors, print the name of the winner, given that both players play optimally.
NOTE: Alice takes the first turn.
Input
The first line of the input line contains T, the number of test cases. Then T lines follow each containing a string denoting the initial state of doors. Open doors are represented by a character '_' and closed doors by a character '|'.
Output
Output T lines containig the name of winner("Alice" or "Bob").
(quotes are for clarity only)
Constraints
1 ≤ T ≤ 20000
The length of string in each case is ≤100.
SAMPLE INPUT
4
|
|||
|__
|__||_|
SAMPLE OUTPUT
Bob
Bob
Alice
Alice
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
We are given a game: there is a line of N doors, each either open '_' or closed '|'. Players alternate turns, with Alice first. In each turn, a player walks up to any closed door (so they must stand at a door that is currently closed). Then they choose a direction (left or right) and start closing consecutive open doors in that direction. They can stop whenever they want, but must close at least one door. If they cannot close any door (i.e., there is no move), they lose.
We need to determine who wins with optimal play.
Interpretation: The state is a binary string: open '_' = open, '|' = closed. Initially some doors are closed. The move: pick a closed door as starting point. From there, go either left or right, and while you encounter open doors, you close them (i.e., change them to closed). You can stop before encountering a closed door, but you must close at least one door (i.e., there must be at least one open door in that direction before hitting a closed door). So essentially, from a closed door, you can select a contiguous segment of open doors that is adjacent to that closed door (to its left or right) and close all or a non-empty prefix of that segment? Let's parse: "He then selects a direction, either left or right, and starts to close the consecutive open doors in that direction. He can stop whenever he wants or if he encounters a closed door. However he must be able to close at least one door." This means that if from that closed door, in the chosen direction, there is at least one open door consecutively before hitting a closed door (or end of line?), then he can close any number of those consecutive open doors, possibly not all of them, but at least one. He stops if he chooses to stop, or if he hits a closed door (so cannot pass through closed doors). He cannot cross a closed door. Also, if he reaches the end of the line? I guess it's fine: if he goes left and there's no door (the leftmost door is index 1), then there is no door to close; that direction yields zero doors, so invalid. So essentially, a move is: choose a closed door at position i. Then choose a direction d (left or right). Let j be the nearest closed door (or boundary) in that direction. Actually, consider positions: doors are indexed 1..N. A door at i is closed. If we go left, we look at positions i-1, i-2,... until we either hit a closed door or beyond the leftmost door. The doors in between (if any) are all open (since we stop at first closed). So there is a contiguous block of open doors adjacent to i on that side. If that block is non-empty (i.e., there exists at least one open door), then the player can close any non-empty subset of that block, but importantly, they must close consecutive doors starting from i's neighbor? The description: "starts to close the consecutive open doors in that direction. He can stop whenever he wants or if he encounters a closed door." This suggests that he must close doors in order, starting from the immediate neighbor and continuing outward. He cannot skip; he can decide to stop after closing any number of consecutive doors from the start. So essentially he can close a prefix of that open segment. He cannot close a suffix or arbitrary subset; only the ones contiguous from the starting closed door. So the move reduces to: pick a closed door i, direction d; let k be the number of consecutive open doors in that direction before hitting a closed door or boundary. Then the player can choose any integer m such that 1 ≤ m ≤ k, and close those m doors (i.e., change those open doors to closed). This will create a larger closed segment? Initially, we have some closed doors. After closing some open doors adjacent to a closed door, the closed door's adjacent block expands. The newly closed doors become part of a contiguous closed segment. So the state evolves.
We need to analyze the impartial combinatorial game.
The game can be seen as a take-away game on intervals? Or perhaps it's equivalent to turning open doors into closed, but the rule about starting at a closed door and closing a contiguous block from there.
We need to find Grundy numbers for the overall state.
Observations: The state can be represented as a string. But maybe we can simplify.
Interpret the game as: There are intervals of open doors separated by closed doors. Initially, there are some closed doors, which act as barriers. The action of closing open doors from a closed door essentially reduces the size of adjacent open intervals, possibly splitting them? Actually, if you close a prefix of an open interval from its left end (if there is a closed door to its left), you shrink that open interval from the left. Similarly from the right. If you close from both sides? But you only act from one closed door and in one direction.
Consider the configuration of closed doors as "walls". Open doors are like gaps between walls. The entire line: consider boundaries as walls too? Because you cannot start at a boundary? Actually, you can only start at a closed door. So boundaries are not closed doors unless there is a '|' at the edge. But they act as barriers because if you go left from the leftmost closed door and there is no door, you can't close any.
Thus the game might be equivalent to playing on independent open intervals, with the ability to reduce the length of an open interval from one side, but only if there is a closed door adjacent on that side. However, the closed doors themselves are part of the state. But as the game progresses, closed doors increase and open intervals shrink and may become zero.
We need to understand impartial game theory for this.
Let's denote positions: A string of length N with characters '_' (open) and '|' (closed). Initially some are closed.
A move: choose a position i with '|' (closed). Then choose left or right. In that direction, let the sequence of consecutive '' (open) starting from i+1 (if right) or i-1 (if left) be of length L>0. Then you may close any number 1..L of them (the ones immediately adjacent to i first). That changes those '' to '|'.
Key: After the move, the closed door at i is still closed, but now the closed block might expand. If you close m doors to the right from i, then positions i+1..i+m become closed. They were open, now closed. This could merge i with the next closed door if m reaches up to a closed door? Actually if there was a closed door at i+m+1, then after closing the m open doors, you'll have contiguous closed doors from i to i+m+? Wait if there is a closed door at i+m+1, that is the boundary of the open segment; you cannot close beyond it because you stop at a closed door. So after closing m (less than L) you don't reach that closed door; after closing exactly L (if L = k, the whole open segment), then you would close up to the next closed door (or boundary). But the rule says you can stop before encountering a closed door or when you encounter a closed door you stop and you don't close that closed door (obviously). But if you close exactly L, you will have closed all open doors up to the next closed door; after that, the two closed doors (i and the next one) become adjacent, merging into a larger closed block. That's allowed.
So essentially the move reduces the open segment on one side of a closed door.
But note: The closed door from which the move is made can be anywhere, not necessarily at the boundary of a closed block? Actually, closed doors can be contiguous. If we have a block of consecutive closed doors, any of them could be chosen as the starting point? However, if you pick a closed door that is interior to a block of closed doors (i.e., both neighbors are closed), then in both directions the immediate neighbor is closed, so you cannot close any door because there is no open door adjacent in either direction (or the first door in that direction is closed). So such a move would be invalid because you must be able to close at least one door. So you cannot pick a closed door that is fully surrounded by closed doors; only closed doors that have at least one open neighbor (i.e., at the boundary of a closed block) are possible move positions.
Thus, the game reduces to considering closed blocks (contiguous segments of closed doors) that separate open intervals. The moves are made from the endpoints of these closed blocks (the leftmost or rightmost closed door of a block) into the adjacent open interval.
Suppose we have an open interval of length L (i.e., L consecutive open doors) between two closed blocks (or between a closed block and the boundary). Actually, boundaries: at the leftmost and rightmost ends of the line, if there is an open interval adjacent to the boundary, there is no closed door on that side to close from. However, could a player start from a closed door at the edge? For example, if the leftmost door is closed, then that closed door has no left neighbor; but it may have a right neighbor open. So it can close to the right. So boundaries are only relevant if there is no closed door adjacent to an open interval on one side. In that case, that side of the open interval cannot be attacked; only the side that has a closed door can be used to close doors from that direction.
Thus each open interval is flanked on left and right possibly by closed blocks (or boundaries). If an open interval is between two closed blocks, then either side can be used to close doors from the respective closed block's endpoint. However, note that the closed blocks themselves might change when doors are closed, merging closed blocks, etc.
But maybe we can view the game as a variant of a Nim-heap where each open interval is a pile of size L, and moves consist of reducing the pile size from one end by some amount, but also possibly merging piles? Actually, when you close doors from one side of an open interval, you reduce its length. If you close all doors from one side, you effectively merge the closed block with the block on the other side (or with the boundary), and the open interval disappears. The closed blocks then become larger, but that might affect the ability to attack adjacent open intervals? For example, suppose we have configuration: closed block A, open interval X, closed block B, open interval Y, closed block C. If you close some doors from the right side of block A into X, that reduces X; if you close all of X, then A and B merge into one larger closed block. That would then combine the two previously separate closed blocks, and perhaps affect the moves on Y? Because now from the right side of the merged block, you could attack Y (from the left). Actually Y is to the right of B (which is now part of the merged block). So after merging, the left side of Y is adjacent to the merged block; it was previously adjacent to B's left endpoint. So essentially the ability to attack Y from the left remains (since B was originally there). So merging doesn't create new attack possibilities except that the endpoints might shift: the left endpoint of the merged block is the left endpoint of A, the right endpoint is the right endpoint of B. But from the perspective of Y, the closed block on its left is now larger, but it still has a leftmost endpoint (which is the rightmost door of B originally) that can attack Y. So the existence of a closed block adjacent to Y on left is unchanged.
Thus, the open intervals are independent? Let's examine: Suppose we have open intervals separated by closed doors. If we close some doors from the left side of an interval (i.e., from the closed block on its left), we reduce the interval length. If we close some from the right side, similarly. However, note that after reducing the interval, the interval still exists (unless it becomes zero). But importantly, the moves that affect one interval do not affect the other intervals except for the fact that the closed blocks between intervals might merge when an interval disappears. But if an interval disappears, then the two closed blocks on its sides merge, but that could potentially join two intervals? No, because the interval between them is gone, they become one closed block. Then any intervals beyond each side remain separate; but there is now a larger closed block separating them? Actually, consider: ... open interval L1, closed block C1, open interval M, closed block C2, open interval R1 ... If M disappears (becomes zero), then C1 and C2 merge into one closed block, call it C. Then we have L1 (left of C) and R1 (right of C). They are still separate intervals; C is a single closed block separating them. So no new interaction. However, if an interval at the boundary disappears (e.g., leftmost open interval is closed from the right by the leftmost closed door), then that closed block might extend to the boundary; but that doesn't affect other intervals.
Thus, it seems that each open interval can be considered as a pile in an impartial game where two players can remove tokens from either end, but only by taking a positive number up to the current size, and the move corresponds to the player who is about to move chooses one of the two ends (if available) and removes some positive number from that end. However, there is a twist: the ability to attack an interval from an end depends on the existence of a closed door adjacent to that end. At the start, each open interval might have 0, 1, or 2 ends adjacent to closed doors (or boundaries). But boundaries are not closed doors, so you cannot attack from a boundary; i.e., if the open interval touches the boundary (i.e., there is no closed door on that side), then that side is not available for moves. Because you need a closed door to start from; if the open interval is at the leftmost, there is no closed door to the left of it. So only the right side (if there is a closed door to the right) can be used. Similarly, if there is no closed door on either side (i.e., the whole string is open? but then there are no closed doors at all, but players must start at a closed door, so if there are no closed doors initially, is any move possible? The problem says "In each turn, the person walks upto any closed door." So if there are no closed doors, there is no move. But is that possible? Initially we have some string; can it contain no '|'? Yes. Then no move possible, so Alice loses (Bob wins) because she can't move? However, the rule: "If he is unable to do so, he must concede defeat." So if there are no closed doors initially, then Alice cannot make a move and loses. In sample input, there are cases with only '_'? They didn't show, but we must consider.
But also, note: At the beginning, there may be closed doors. After moves, more closed doors appear. But even if initially there is an open interval with no closed door on one side (because it's at an edge), after some moves, could a closed door appear on that side? For an open interval at the left edge, the only way to get a closed door to its left is to close doors from a closed door that is to the left, but there is none. So the left side will never get a closed door because we can't create a closed door out of nowhere beyond the edge. Thus, that side remains unavailable. So each open interval has a fixed set of "attackable" ends: ends that are adjacent to a closed door (or that will always be adjacent to a closed door? Actually, if we have an open interval between two closed blocks, both ends are attackable. If it's at an edge with a closed block on the interior side, only the interior side is attackable. If it's at an edge with no closed block (i.e., the whole string is open, and there is no closed door at all), then there is no closed door anywhere, so there is no move. But if the whole string is open, no moves, Alice loses. Similarly, if there is a single closed door somewhere, then there are two open intervals on its sides (possibly of zero length if at boundaries). Those intervals may have attackable ends: for the left interval, its right end is adjacent to the closed door, so attackable from the right (starting from that closed door moving left). Its left end is the boundary, not attackable. So it's like a half-open interval.
Thus, the game might decompose into independent subgames: each maximal contiguous segment of open doors that is flanked by closed doors or boundaries. However, we must also consider that moves can be made from closed doors that are at the boundaries of closed blocks, and those moves affect exactly one adjacent open interval. So it's like each open interval is a pile that can be reduced from either end if that end is "active" (adjacent to a closed door). The closed doors themselves are not piles but serve as "tokens" to allow moves. But note: When you close doors from a closed door, that closed door remains; the move doesn't change the fact that there is a closed door at that endpoint. So the ability to attack the open interval from that side persists until the interval disappears. Moreover, after you reduce the interval from one side, the closed door that you used to attack is still there at the same position, but the distance to the other side changes. Actually, if you close m doors to the right from a closed door at position i, you extend the closed block to the right. The closed door at i is still there, and now the new rightmost closed door of that block is at i+m. The leftmost closed door is unchanged (if you attacked leftwards, the leftmost changes). So the endpoint from which future attacks on that same side can be made shifts: after closing m doors to the right from the left closed block, the rightmost closed door is now at i+m. So future attacks on that same open interval from the left side would now start from the new rightmost closed door (the one at the edge of the block). But the new rightmost closed door is adjacent to the remaining open interval. So effectively, the side from which attacks are possible moves inward as you close doors.
Thus, the open interval can be thought of as a segment of length L. There are two "markers" indicating the positions of the adjacent closed doors (or boundaries). If there is a closed block on the left, its rightmost closed door is at some position, which we can consider as a pointer to the left boundary of the open interval. Similarly, if there is a closed block on the right, its leftmost closed door is at the right boundary. Initially, these pointers are at the edges of the open interval. A move from the left side: the player chooses a number 1..k where k is the current length of the interval (distance to the right marker), and reduces the interval length by moving the left pointer to the right by that amount. That is, the left pointer advances (closing doors) into the interval. After the move, the interval length becomes L' = L - m (where m chosen). Similarly, a move from the right side reduces L by moving the right pointer leftwards.
If only one side is active (edge), then the interval is like a stack that can only be reduced from one side.
Thus, the game for each open interval is exactly a take-away game where players alternately reduce a positive integer L by removing any positive amount from one of the ends, but only if that end is allowed. In the case both ends allowed, it's like a game of a pile where you can take any positive number from either end. However, note that the reduction operation from one end is like subtracting a positive integer from L, but actually, after moving the left pointer, the distance between the pointers shrinks by exactly that amount. It doesn't affect the other pointer. So the state is simply the current length L (a nonnegative integer), with two flags: can we move from left? (i.e., is there a closed door on left) and can we move from right? (closed door on right). However, there is a nuance: after a move from the left, the left side remains active (since the new leftmost closed door is still there) and the right side remains as it was. So the flags don't change: if an interval initially has both sides active, it remains both sides active until it disappears (L=0). Because after any move, we still have a closed block on that side (the one we used to attack) and the other side's closed block unchanged. Actually, check: Suppose an interval between two closed blocks. Left closed block has rightmost closed door at position p; right closed block has leftmost closed door at position q, with open doors from p+1 to q-1. If Alice makes a move from the left block, she closes m doors from p+1 to p+m. After that, the left block's rightmost closed door is now at p+m. So still there is a closed block on left. Right block unchanged. So both sides remain active. So yes, the flags persist. If L becomes zero, then the two closed blocks meet, and the interval disappears, merging into a larger closed block. But that doesn't affect other intervals.
What about an interval at an edge with no closed door on left? Initially left side is boundary. Only right side active. After a move from the right side (the only active side), we close m doors from the right side moving leftwards. That reduces L, and the right block extends leftwards; the left side remains boundary (still inactive). So flag remains the same.
Thus, each open interval is an independent impartial game with state (L, a, b) where a and b are booleans indicating whether left or right side attacks are possible. The initial a,b are determined by the neighbors: if the open interval touches the left boundary, then a = false (cannot attack from left); if it has a closed door to its left, a = true. Similarly for right. The game ends when all intervals have L=0? Actually, the overall game ends when no moves are possible. Moves are possible if there exists any open interval with L > 0 and at least one active side. If all L are zero, then there are no open doors; then the only closed doors are all closed. But could there be a move then? Possibly if there is a closed door with an open neighbor, but if all open doors are gone, then there is no open neighbor, so no moves. So the terminal position is when every open interval length is 0. Also, if there are closed doors but no open doors, no moves. So the game is a disjunctive sum of these interval games.
Therefore, we need to compute the Grundy number (nimber) for an interval of length L with flags (a,b). Then the overall Grundy number is XOR over all intervals. If XOR != 0, Alice wins; else Bob.
Now we need to derive Grundy values for the game "length L, can reduce by any positive amount from left if a=true, from right if b=true". This is akin to a subtraction game where you can subtract any positive integer up to L from either side (if allowed). But note: when you reduce from left, you effectively take from the left side; but the resulting state is an interval of length L - m, with same flags a,b? However, if you take from left, does the left flag remain active? Yes, because the left side still has a closed block (the one you used) after the move. So the state after move is (L-m, a, b). Similarly from right yields (L-m, a, b). So the moves are symmetric.
But wait: is there any move that changes the flags? Consider an interval at edge: a=false, b=true. After a move from right, we close m doors from right, so the interval shrinks, but still a=false (left boundary) and b=true (right closed block still present). So flags unchanged.
Thus, the Grundy number g(L, a, b) depends only on L and the pair (a,b). Since the flags are symmetric if we swap left and right, g(L, true, false) should equal g(L, false, true) by symmetry.
Now, what are possible (a,b) cases?
Case 1: both sides active (true, true). Denote this as "two-sided".
Case 2: only one side active (true, false) or (false, true). Denote as "one-sided".
Case 3: both sides inactive (false, false). This would be an open interval with no closed doors on either side. But can this happen? Initially, if there is a maximal open segment that is not adjacent to any closed door? That would mean the entire string is open, and there are no closed doors at all. In that case, there is exactly one open interval covering the whole string, and it has a=false, b=false because there are no closed doors. But then the whole game has no moves because you need a closed door to start. So this is a terminal position? Actually, if there are no closed doors, there is no starting point. So no moves. So the state is terminal, Grundy 0. But does our decomposition consider this? We can treat it as an interval with L>0 but both flags false, which indeed has no moves because you cannot start from any closed door (there are none). So Grundy=0. However, this is consistent: g(L, false, false) = 0 for any L, because no moves. But wait: Is it possible to have such an interval in a larger game where there are other closed doors? If there are closed doors elsewhere, any maximal open segment must be adjacent to closed doors on at least one side? Because it's maximal: if there is a closed door somewhere, then the open segment is bounded by that closed door or boundary. For an open segment not adjacent to any closed door, it would mean there is a closed door elsewhere but the segment is separated by closed doors? Actually, by definition, maximal contiguous open doors are separated by closed doors. So each such segment is between two closed doors or between a closed door and a boundary. So it always has at least one adjacent closed door, unless the whole string is open (no closed doors). So the case (false,false) only occurs when there are no closed doors at all. So we can ignore for other states.
Thus, the game reduces to a set of independent piles, each pile is a number L with move options: subtract any positive integer k (1 ≤ k ≤ L) if the side flag is true. But note: In the two-sided case, you have two sets of moves, but both lead to states (L - k, true, true). So effectively, the move set from state (L, true, true) is: for each k from 1 to L, you can move to (L - k, true, true). Similarly, for (L, true, false), moves: for each k from 1 to L, move to (L - k, true, false). So in both cases, the set of reachable Grundy numbers is the set of g(L - k) for k=1..L (with the same flags). So it's essentially the same as a simple subtraction game where you can remove any positive number of tokens from a single heap. The Grundy number for such a game is well-known: for a heap of size n where you can take any positive number (i.e., subtract any amount), the Grundy number is n (since you can move to any size from 0 to n-1). More precisely, the mex of {g(0), g(1), ..., g(n-1)} yields g(n)=n. Because g(0)=0, then g(1)=mex{g(0)}=mex{0}=1, g(2)=mex{g(0),g(1)}=mex{0,1}=2, etc. So indeed, for a game where you can reduce the heap to any smaller nonnegative integer, the Grundy number is the heap size.
Thus, for one-sided interval, g(L, true, false) = L? But careful: Is L allowed to be 0? If L=0, the interval is empty, Grundy=0 (no moves). For L>0, moves: to any L' < L with same flags. So yes, g(L) = L. Similarly for two-sided interval, the moves are exactly the same set: you can reduce L to any smaller L' (since you can take any number from left or right; but taking from left or right both yield (L - k, true, true) with same flags). So the move set is exactly the same: from L you can go to any L' from 0 to L-1. Therefore, g(L, true, true) = L as well! So it would seem that both types give Grundy = L. If that's the case, then the whole game's outcome is determined by XOR of lengths of all open intervals. But is that correct? Let's test with sample.
Sample Input:
4
| -> string: '', '|', '' . So N=3. Open intervals: left of closed door: one open door at position 1, length 1, right side active? Actually, left interval: it's between left boundary and the closed door. So left interval has left inactive (boundary), right active (closed door on right). So it's one-sided, length 1. Right interval: similar, length 1, left active, right inactive. So two intervals each of length 1, one-sided each. XOR = 1 XOR 1 = 0 -> Bob wins. Sample output: Bob. Good.
Second: ||| -> all closed, no open intervals? Actually all doors are closed. Then there are no open doors. So there are no moves? Wait, if all doors are closed, can a player make a move? They need to pick a closed door and then close open doors. But there are no open doors; so from any closed door, both directions have either closed or boundary. So no open doors to close; cannot close at least one door. So no moves. Alice loses -> Bob wins. XOR of no intervals = 0 -> Bob. Good.
Third: |__ -> string: '|', '', '' . So closed door at left, then two open. Open interval: from position 2 to 3, between closed door on left and right boundary. So it has left active (closed door on left), right inactive (boundary). So it's one-sided, length 2. XOR = 2 -> non-zero, Alice wins. Sample output: Alice. Good.
Fourth: |||| -> Let's parse: characters: index:1 '|', 2 '', 3 '', 4 '|', 5 '|', 6 '', 7 '|'? Actually string: "||||". Let's count: 1:|, 2:, 3:, 4:|, 5:|, 6:, 7:|. So doors: closed at 1, open 2-3, closed at 4, closed at 5, open at 6, closed at 7. Now we need to identify maximal open intervals. Open intervals:
Between closed at 1 and closed at 4? Actually, after closed at 1, we have open at 2-3, then closed at 4. So interval A: positions 2-3, length 2, left active (closed at 1), right active (closed at 4). So two-sided, length 2.
Between closed at 4 and closed at 5? There is no open between 4 and 5 because they are consecutive closed? Actually 4 is '|', 5 is '|', adjacent, so no open.
Between closed at 5 and closed at 7? We have open at 6, length 1, left active (closed at 5), right active (closed at 7). So two-sided, length 1.
Also after closed 7? No.
Before closed 1? left boundary, none.
Thus intervals: length 2 (two-sided), length 1 (two-sided). XOR = 2 XOR 1 = 3 (non-zero) -> Alice wins. Sample output: Alice. Good.
So the simple XOR of lengths rule matches all samples.
But wait, are there any subtle cases where the two-sided interval might have a different Grundy because of some nuance? For example, consider an interval with both sides active, could there be a move that changes the flags? No, as argued. So it seems that from state (L, true, true), the set of moves is exactly to (L - k, true, true) for any k from 1 to L. Because you can choose left direction and close k doors, resulting in new length L-k, and both sides still active (left side still has a closed block, right side unchanged). Or choose right direction, same outcome. So indeed moves are exactly the subtraction moves.
But is there any move that could result in a state where one of the flags becomes false? For that to happen, the open interval would need to become adjacent to a boundary? But if originally it's between two closed blocks, after closing some doors, it's still between two closed blocks, unless one side's closed block disappears? Could a closed block disappear? Closed blocks are collections of closed doors; they never disappear because we never open doors. So they always exist. So the flags remain true. So yes.
Now, consider an interval adjacent to a boundary and a closed block: one-sided. After a move from the active side, it remains one-sided. Because the boundary remains, closed block remains.
Thus, g(L) = L for any interval that has at least one active side. For intervals with no active sides (only possible when whole string open), g=0. So the total Nim sum is sum of lengths of open intervals that are adjacent to at least one closed door. But note that if there is any closed door, then every maximal open interval is adjacent to at least one closed door? Let's check: If there is at least one closed door, then any maximal open segment either touches a closed door on at least one side because otherwise it would be isolated by closed doors? Actually, consider a string with a closed door somewhere; then the open segment to its left (if any) is adjacent to that closed door on the right side; the open segment to its right is adjacent on the left side. So yes, each open segment will have at least one adjacent closed door. The only exception might be if the closed door is at the very left, then the right open segment is adjacent to it, and the left open segment is nonexistent (boundary). So all open segments have at least one active side. Therefore, in any non-trivial game (with at least one closed door), the Grundy number for each open segment is its length.
But wait, what about an open segment that is between two closed doors? That has two active sides. We argued g = L. That's fine.
Thus, it seems the game reduces to Nim where each open segment is a pile of size equal to its length. The winning condition is XOR of all open segment lengths != 0.
But is there any scenario where moves from a closed door could affect more than one open segment simultaneously? For instance, if you start at a closed door that is in the middle of a closed block? But as argued, you can only start at a closed door that has an open neighbor, i.e., at an endpoint of a closed block. Those endpoints are adjacent to exactly one open interval (the one on that side). Closing doors from that side only affects that interval. However, what about the case where the closed block consists of a single closed door. That closed door is adjacent to up to two open intervals (left and right). But if you start at that closed door, you must choose a direction; you can only affect one interval. So still, a move only affects one interval.
But what about a closed block with more than one door? The endpoints are distinct. Starting at an endpoint, you can only affect the interval adjacent to that endpoint. So still independent.
Thus, the intervals seem independent.
But let's consider a potential interaction: Suppose we have two open intervals separated by a closed block. Could a move from one side of the closed block affect the other side indirectly? For example, if we close doors from the left endpoint of the closed block, we extend the closed block to the right, thereby reducing the left open interval. That does not affect the right open interval. So independent.
Hence, the impartial game is the disjunctive sum of independent impartial games, each being a heap of size L with move set: remove any positive number of tokens. That's Nim heap of size L.
Thus, the Grundy number for a heap of size L is L (since from size n you can move to any size 0..n-1). So overall Grundy = XOR of all L.
Therefore, the winner is Alice if XOR != 0, else Bob.
But wait, is there any nuance about the rule "If he is unable to do so, he must concede defeat." That is just normal losing condition when no move.
Now, need to verify with more test cases to ensure no hidden twist. Let's consider some edge cases:
Case: Single closed door in middle with open on both sides: e.g., "|" we already did; lengths 1 and 1 -> XOR 0 -> Bob.
Case: "|_" (closed, open). Length 1, one-sided -> XOR=1 -> Alice. Let's simulate: Alice can close the open door (choose closed door at position 1, direction right, close 1 door). After that, all doors closed, Bob has no move -> Alice wins. Yes.
Case: "_|" (open, closed). Similar, Alice can close the left open door from the closed door moving left, wins.
Case: "||": closed, closed, open. Here we have one open interval at right, length 1, but left side active? The open interval is adjacent to the closed door at position 2? Actually, doors: index1 '|', index2 '|', index3 ''. The maximal open interval is positions 3 only, adjacent to closed door at position 2 (since position2 is closed). However, is there a closed door at position 2 that can be used? Yes, the closed door at position 2 has an open neighbor to the right. But also position1 is closed but its right neighbor is closed (position2), so it cannot be used. So only possible moves are from the closed door at position 2, direction right, close 1 door. So Grundy length 1 -> Alice wins. Simulate: Alice closes the open door, all closed, Bob loses. Yes.
Case: "_||": open, closed, closed. Similar, Alice wins.
Case: "|_|": closed, open, closed. Length 1, two-sided? Actually open interval length 1 between two closed doors. According to our rule, g=1, XOR=1 -> Alice. Simulate: Alice picks either closed door, direction toward the open door, closes it. Then all closed, Bob loses. So Alice wins.
Case: "|__|": closed, open, open, closed. Length 2, two-sided. XOR=2 -> non-zero, Alice wins. Let's simulate: Alice has options: from left closed door, she can close 1 or 2 doors to the right; from right closed door, she can close 1 or 2 doors to the left. Suppose she closes 1 door from left: then state becomes: left closed door, then closed at position2 (new), open at position3, closed at position4. So now we have open interval of length 1 between closed at pos2 and closed at pos4? Actually, after closing position2, the configuration: pos1 '|', pos2 '|' (now closed), pos3 '_', pos4 '|'. So intervals: between pos2 and pos4, length 1. So it's now a length-1 interval (two-sided? left closed at pos2, right closed at pos4). So Bob's turn with a heap of size 1. Bob can close that open door and win? Let's see: after Bob closes the last open door, all closed, then Alice has no move and loses, so Bob wins. So if Alice reduces 2 to 1, she loses? But we predicted Alice wins from initial position. So maybe Alice has a winning move: she should close 2 doors from left (or right), i.e., close both open doors. Then state all closed, Bob has no move, Alice wins. So indeed, Alice can win by taking all. But suppose she takes 1, she loses. So optimal play: she will take all. So Alice wins. So consistent.
Now consider a more complex configuration with multiple intervals: e.g., "||_". That's open, closed, open, closed, open. Open intervals: left of first closed: length1; between first and second closed: length1; right of second closed: length1. So three piles of size1 -> XOR = 1 XOR 1 XOR 1 = 1 -> Alice wins. Simulate? Possibly.
Now consider a configuration where lengths produce XOR zero: e.g., "|__|": open1 length1, between closed doors length2, right length1. So lengths: 1,2,1 -> XOR = 1 xor 2 xor 1 = 2 -> non-zero, Alice wins? But 1 xor 2 xor 1 = 2, yes non-zero. For XOR zero, need something like lengths 1,2,3? 1 xor 2 xor 3 = 0. So configuration like: left length1, middle length2, right length3? But that would be something like: open1, closed, open2 (len2), closed, open3 (len3). That is possible: "||". Lengths 1,2,3 XOR = 0. So Alice should lose if both play optimally. Let's try to simulate mentally to see if that holds.
Configuration: positions: 1 '', 2 '|', 3 '', 4 '', 5 '|', 6 '', 7 '', 8 ''. So open intervals: A: [1] length1 (left of first closed, right active), B: [3-4] length2 (two-sided), C: [6-8] length3 (right of second closed, left active). According to Nim theory, XOR=0 -> losing for player to move. So Alice should lose.
We can attempt to find a strategy for Bob to maintain zero.
Let's test some moves. Alice can choose any interval and reduce its length by any amount (from either active side). She must choose a closed door adjacent to that interval.
If she reduces interval A from length1 to 0 by closing that door (using the closed door at position2 leftwards), then A disappears. New state: closed door at pos1? Wait, after closing A, she closes the open door at pos1 from the closed door at pos2 moving left. So pos1 becomes closed. So now we have: positions 1 closed, 2 closed, 3-4 open, 5 closed, 6-8 open. But note: after closing pos1, the closed block at left now comprises positions 1 and 2. So interval B is now between closed block (positions1-2) and closed at pos5. So B remains length2. Interval C remains length3. So lengths: B=2, C=3 -> XOR=2 xor3 =1 (non-zero), so Bob gets a winning position? Actually, if it's a losing position for the player who just moved, then Alice's move to this state would be winning for Bob? Wait, after Alice moves, it's Bob's turn. If the resulting XOR is non-zero, then Bob (next player) has a winning move if he plays optimally. So Alice's move to a non-zero state is not necessarily losing for her if she can force a win? In impartial games, if you move to a position with non-zero Grundy, the next player can win if they play optimally (since they can move to a zero position). So from a zero initial position, any move leads to non-zero, and then opponent can force a win. So indeed, if initial XOR=0, Alice loses with perfect play. So we need to check that in this example, Bob can force a win.
Let's see if Bob can always respond to bring XOR back to zero. Suppose Alice reduces interval B from length2 to length1 (by taking 1 from either side). Then lengths become: A=1, B=1, C=3 -> XOR = 1 xor1 xor3 = 3 -> non-zero. Bob would then try to move to zero. Could Bob reduce something to make XOR=0? For example, from (1,1,3), the XOR is 3. To get zero, Bob needs to change one pile such that new XOR=0. So he needs to reduce the pile of size 3 to size 3 xor 3 = 0? Actually, if current XOR = S, to make it zero, he needs to find a pile with size >= S and reduce it to size xor S? Specifically, choose a pile of size x such that x xor S < x, and reduce it to x xor S. Here S=3. Piles: 1,1,3. Check pile of size 3: 3 xor 3 = 0, which is less than 3, so Bob can reduce the 3-pile to 0 by closing all doors in interval C (from the adjacent closed door). That would result in lengths (1,1,0) -> XOR=0. So Bob can do that. So after Alice's move to (1,1,3), Bob can close all three doors in C, leaving only A=1, B=1, and then Alice gets XOR=0 and loses. That seems plausible.
But let's verify that Bob can indeed close all three doors in C in one move. Interval C is of length 3, adjacent to closed door at position5 on the left, and boundary on the right? Actually, C is to the right of closed door at position5, and there is no closed door to the right (boundary). So it's one-sided (left active). Bob can choose the closed door at position5, direction right, and close 3 doors (positions6,7,8). That's allowed because there are 3 consecutive open doors before hitting boundary (end). So yes. So Bob wins.
Thus, the Nim analogy holds.
Now, what about an interval with both sides active, but could there be a move that reduces the interval by taking from one side but also somehow changes the "active side" status of the other side? Suppose we have two closed blocks and an open interval between them. If we close from left side, the left closed block expands. That doesn't affect the right closed block. Both sides remain active.
But is there any scenario where after a move, an interval splits into two intervals? For example, starting from a configuration where there is a closed block and an open interval, could a move create a new open interval? Suppose we have a closed block, then an open interval, then another closed block. If we close not from the endpoints but from a closed door inside the block? But we can only close from endpoints because interior closed doors have no open neighbors. So no.
What about if we close from a closed door that is adjacent to two open intervals (like a single closed door between two open intervals). After closing from one side, we reduce that side's interval, but the closed door becomes part of an extended closed block on that side. However, the other side's interval remains. But could the move affect the other side's interval? Possibly if we close enough to merge the closed door with the other closed block? For example, consider: open left interval L, closed door C, open right interval R. If we close from C moving left, we close some doors from L. After closing, the closed block on left expands to include those doors, but C remains a closed door. The right interval R remains unchanged and still adjacent to C. So still two intervals? Actually, originally we have two intervals: left interval (adjacent to C on right) and right interval (adjacent to C on left). But are they considered separate intervals? In our decomposition, we treat each maximal contiguous open segment. In this configuration, there are two maximal open segments: one left of C, one right of C, separated by the closed door C. They are distinct intervals. So after closing some doors from the left interval, we still have two intervals: left reduced, right unchanged. So no merging of intervals unless one interval disappears entirely. So the number of intervals can only decrease, never increase.
Thus, the impartial sum is straightforward.
Now, we need to consider if there is any possibility of a move that simultaneously affects two intervals? For instance, if we have a closed block consisting of multiple doors, could we start at an interior closed door and close open doors on both sides? The rule says you select a direction, either left or right. So you can only go one direction. So not both.
Thus, the game is exactly Nim on pile sizes equal to lengths of open intervals.
But wait: What about the case where there is a closed door but no open doors? Then no moves; Grundy=0. That matches Nim with no piles (XOR=0).
So solution appears to be: For each test case, compute the lengths of maximal contiguous segments of '' (open) that are adjacent to at least one '|'? Actually, if there is at least one '|', then all '' segments are adjacent to a '|' because they are separated by '|' or at boundaries with '|'? Check: Could there be an open segment that is not adjacent to any '|'? Only if there are no '|' at all, in which case the whole string is open. But if there is at least one '|', then any maximal open segment is either between two '|' or between a '|' and boundary. In both cases, it is adjacent to at least one '|'. So we can simply take all maximal '_' segments. Their lengths are the pile sizes. Compute XOR. If XOR != 0, Alice wins; else Bob.
But wait: What about the scenario where the open segment is at the boundary and there is no '|' on that side, but there is a '|' on the other side. That's still one active side. That's fine.
Thus, solution is straightforward.
But we must confirm that there is no move that can be made from a closed door that is not at an endpoint of its block? As argued, such a closed door would have both neighbors closed (or boundary and closed), so cannot close any open door because no open door adjacent. So not a legal move.
Thus, our analysis seems correct.
But before finalizing, let's explore potential pitfalls.
The move description: "In each turn, the person walks upto any closed door. He then selects a direction, either left or right, and starts to close the consecutive open doors in that direction. He can stop whenever he wants or if he encounters a closed door. However he must be able to close atleast one door." This suggests that after he stops (or encounters closed door), he has closed all doors he passed until that point? Or he can stop earlier. So it's as we modeled.
But what about the possibility of "jumping" over closed doors? No.
Another nuance: When he walks up to a closed door, does he stand at that door? He is at that door, then moves in the chosen direction closing doors. He closes doors as he goes. After closing, those doors become closed. The starting closed door remains closed.
All good.
Now, is there any restriction that he cannot close doors that are not directly adjacent to a closed door? But he closes consecutive open doors starting from the immediate neighbor, so it's fine.
Thus, the impartial game is as described.
But wait, is there any difference between one-sided and two-sided intervals in terms of Grundy? We argued moves from two-sided are still just reductions. However, consider the two-sided interval of length L: can we always reduce it to any L' < L? Yes, by taking from either side. For L' = 0, we need to close all L doors. That is possible from either side: you can take L from left (close all doors) as long as there are L consecutive open doors in that direction. Since the interval is exactly those L doors, closing from left will close all up to the right closed door? But if you take L from left, you'll close all L open doors, and you'll encounter the right closed door at the end (since the rightmost neighbor is a closed door). The rule says you stop if you encounter a closed door, but you don't close it. So you can close exactly L doors, and then you stop because the next door is closed. That's allowed. So yes, you can reduce to 0. Similarly from right. So moves cover all k from 1 to L.
But what about moving from a one-sided interval? Can we reduce it to 0? Yes, by taking all doors from the active side. So same.
Thus, Grundy = L.
But is there any subtlety about the fact that when you close doors from one side, the other side's closed door remains at the same distance? That doesn't affect moves.
Hence, the game is essentially Nim.
But wait, one might think of the impartial game as a take-away game on a heap, but there is a known nuance: In Nim, you can remove any number from a heap, but the heap is independent. Here, the intervals are independent as we argued.
Now, let's test with some random small cases by brute force to be absolutely sure. Since N <= 100, we could in principle simulate all positions to verify Grundy numbers. But we need to reason.
Consider a configuration: "|||" -> alternating. That yields open intervals: lengths: 1,1,1,1? Actually, string: _ | _ | _ | _. There are 4 open doors? Let's list: positions: 1 , 2 |, 3 , 4 |, 5 , 6 |, 7 . So open intervals: before first closed: [1] len1; between 2 and 4: [3] len1; between 4 and 6: [5] len1; after last closed: [7] len1. So four piles of size1 -> XOR=0 -> Bob wins. Is that plausible? Let's simulate: Alice must choose a closed door (positions 2,4,6) and close an adjacent open door. Suppose she chooses closed at 2, direction left to close door1. Then state: closed at 1? Actually, door1 becomes closed. Now config: | | _ | _ | _ (positions 1 closed,2 closed,3 open,4 closed,5 open,6 closed,7 open). So open intervals: [3] len1 (between closed 2 and 4), [5] len1 (between 4 and 6), [7] len1 (right of 6). That's three piles of size1 -> XOR=1 -> Bob's turn with non-zero. Bob can then make a move to bring XOR to 0. For example, Bob can close door3 from closed door2 right? Actually, closed door2 has neighbor open at 3; he can close it, making all from 1-3 closed. Then state: closed 1,2,3,4,5 open? Wait, after closing door3, config: | | | | _ | ? Let's be careful: Starting after Alice's move: doors: 1 '|', 2 '|', 3 '', 4 '|', 5 '', 6 '|', 7 ''. Bob can pick closed door at 2, direction right, close door3. Then doors: 1 '|',2 '|',3 '|',4 '|',5 '',6 '|',7 ''. So open intervals: [5] len1, [7] len1 -> XOR=0. Then Alice's turn with XOR=0. She moves, etc. It seems plausible that Bob can force win. So consistent.
Now consider a configuration: "__|" -> open open closed open. So intervals: left of closed: length2 (one-sided, active right), right of closed: length1 (one-sided, active left). So piles: 2 and 1 -> XOR=3 -> Alice wins. Simulate: Alice could take all from left (close both left doors) resulting in all closed? Actually, if she closes both left doors from closed door at position3 moving left, then doors: positions 1 and 2 become closed, so config: | | | ? Wait, initial: 1 '', 2 '', 3 '|', 4 ''. After Alice closes two left doors (positions 2 and 1) from door3 leftwards? But she must close consecutive open doors starting from immediate neighbor: from door3, left neighbor is door2, then door1. She can close both, so doors 2 and 1 become closed. Then config: 1 '|', 2 '|', 3 '|', 4 ''. Now only open door is at 4, adjacent to closed at 3. So it's a pile of size1 for Bob. Bob closes it and wins. So that move leads to loss for Alice. But Alice could instead close just one left door, reducing left pile from 2 to 1. Then state: 1 ''? Let's simulate: Alice closes door2 only (one left). Then config: 1 '', 2 '|', 3 '|', 4 '_'. So open intervals: left interval length1 (door1), right interval length1 (door4). So piles: 1 and 1 -> XOR=0. Then Bob's turn with XOR=0, which is losing for the player to move, so Bob loses if Alice plays optimally afterwards. So Alice wins. So indeed, Alice has a winning move: reduce the left pile from 2 to 1, making XOR zero. So matches Nim.
Thus, the solution is robust.
Now, we need to implement for T up to 20000, each string length <=100. Simple.
Algorithm: For each test case string s:
If there is no '|' in s, then there are no closed doors, so Alice cannot move => Bob wins. But according to our pile approach, there is one open interval of length N but with no adjacent closed doors, which we treat as Grundy 0. So XOR=0 -> Bob. So we can just compute as described.
Else, find all maximal segments of '_' (open). For each such segment, compute its length. Compute XOR of all lengths. If XOR != 0 -> Alice else Bob.
But careful: What about an open segment that is adjacent to a '|' on one side? That is always the case if there is any '|' in the string, except maybe if the open segment is at the very beginning and there is no '|' to the left? But there is a '|' somewhere, but not necessarily adjacent on the left if the leftmost part is open and the first closed door is somewhere to the right. That open segment is adjacent to a '|' on its right side. So it's active. So all open segments in a string containing at least one '|' are active (have at least one adjacent closed door). So we can just take all '_' segments.
But is there any case where a '' segment is not adjacent to any '|' even though there is a '|' elsewhere? That would require the '' segment to be surrounded by boundaries on both sides? That would be the only segment and the string contains '|'? If the string contains a '|', then the string is partitioned by '|'. The leftmost segment (if open) is adjacent to a '|' on the right (if not the whole string is open). The rightmost segment is adjacent to a '|' on the left. So always adjacent.
Thus, we can simply compute XOR of lengths of all maximal '_' segments.
Edge case: No '_' at all (all '|') -> no open segments -> XOR=0 -> Bob. That matches.
Thus, solution is straightforward.
But wait, consider a string like "|". That's closed then open. Our algorithm: find '' segments: one segment of length 1. XOR=1 -> Alice. Good.
Now, does the order of moves or the fact that players must start at a closed door affect the ability to close doors from a particular side of an open segment? For a given open segment, there might be two closed doors adjacent to it (one on each side). But to affect that segment, you need to start at one of those adjacent closed doors. Are those closed doors always available? They are part of the state and are closed. They might be interior to a larger closed block; but the endpoint of that block is adjacent to the open segment. However, if the closed block is of length >1, the endpoint that is adjacent to the open segment is a closed door with an open neighbor, so it's a valid starting point. So yes.
Now, consider a situation where an open segment is adjacent to a closed block that has multiple closed doors; the endpoint is the door at the boundary. That endpoint is a closed door, so it's available. So always.
Thus, we can treat each open segment as an independent Nim heap with size equal to its length.
Now, I want to double-check with some more complex configurations that might reveal hidden dependencies. For instance, consider two open segments separated by a closed block of length >1. Could a player, by closing doors from the interior of that closed block, affect something? They can't because interior closed doors have no open neighbors. So no.
What about the possibility of a move that simultaneously affects two open segments? Suppose we have a closed block of length 1, i.e., a single closed door between two open segments. If we start at that closed door, we must choose a direction. So only one segment is affected. So no.
Thus, the game is a sum of independent heaps.
Now, we should confirm the Grundy number for a heap where you can subtract any positive integer is indeed the heap size. The mex of {g(0), g(1), ..., g(n-1)} yields n if g(k)=k for all k<n. This holds if the move set is exactly all smaller nonnegative integers. However, we must ensure that from a heap of size n, you can move to any size m where 0 ≤ m < n. In our game, you can choose to close k doors, where 1 ≤ k ≤ n, resulting in new size n-k. So yes, you can achieve any size from 0 to n-1. So Grundy = n.
Thus, XOR of lengths.
Now, the sample matches.
Thus, answer: For each test case, compute the XOR of lengths of contiguous '_' substrings. If XOR != 0, print "Alice", else "Bob".
But we need to be cautious about the scenario where there are no closed doors at all. In that case, there is a single '' segment of length N, but according to our rule, that's a heap of size N. However, is it really a heap? Because there are no closed doors, you cannot make any move. So the Grundy number should be 0, not N. So we must exclude open segments that are not adjacent to any closed door. In a string with no '|', the open segment is the whole string, and it has no adjacent closed doors. So it should be considered as having Grundy 0. In our algorithm that simply takes all '' segments, we would include it and get XOR = N, which would incorrectly predict Alice wins when N>0. But if there are no closed doors, Alice cannot move, so Bob wins. So we must treat that case separately. However, does the problem statement allow a string with no '|'? The string can consist of '' and '|', so yes, it's possible. For example, all ''. Then Alice cannot make a move (since no closed door to start from), so Bob wins. So our algorithm must handle that: if there is no '|' in the string, answer "Bob". Otherwise, compute XOR of lengths of all '_' segments (which will all be adjacent to a '|' because at least one '|' exists). But wait, what if there is at least one '|', but also there is an open segment that is not adjacent to any '|'? As argued, that's impossible because any maximal open segment in a string containing at least one '|' must be bounded by either boundary or '|'. If it's bounded by boundary on one side, it must be bounded by a '|' on the other side (unless the string is entirely open). So it will have at least one adjacent '|'. So it's active.
Thus, correct algorithm: For each test case string s:
If there is no '|' in s: print "Bob"
Else:
Parse s to find maximal consecutive '_' sequences.
Compute the XOR of their lengths.
If XOR != 0: print "Alice" else "Bob".
Check sample: The sample includes no all '_' case, but we need to handle.
But what about a string like "|"? That has a '|', so active. Lengths 3 and 3 -> XOR=0 -> Bob. Let's simulate to see if Bob wins. Initial: three open left, closed, three open right. According to Nim, losing for first player. Let's test small: Suppose left=1, right=1 we already saw Bob wins? Actually, "|" gave XOR=0, Bob wins. For 3 and 3, XOR=0. Could Alice have a winning move? She could reduce one heap from 3 to something else. Suppose she reduces left to 0 (close all three left). Then state: all closed left, and right remains 3 -> XOR=3 -> Bob's turn with a winning position. Bob can then reduce the right to 0 and win. If she reduces left to 1, then lengths 1 and 3 -> XOR=2, Bob can reduce the 3 to 1 (since 3 xor 2 = 1) giving lengths 1 and 1 -> XOR=0, etc. So seems losing. So consistent.
Now consider a string like "|___" (closed then three open). That's one heap of size 3, XOR=3 -> Alice wins. She can win by making XOR 0: reduce 3 to 0 (take all). So yes.
Thus, algorithm is solid.
But we must double-check the possibility of a move from a closed door that is not at the boundary of its closed block but still has an open neighbor? Could there be a closed block that has an open neighbor on both sides? That's just a single closed door. For longer blocks, only the ends have open neighbors. So endpoints are the only viable starting points.
Now, consider a string like "|__|" we already considered (1,2,1) XOR=0 -> Bob. We tested that Alice's move to (0,2,1) yields XOR=3 (since 0 xor2 xor1=3) and Bob can win. But what if Alice reduces the middle from 2 to 0? That yields (1,0,1) XOR=0? 1 xor 0 xor 1 = 0. Wait, 2 to 0 is possible by closing both doors from either side. Then state: open left length1, middle gone, open right length1. So lengths 1 and 1 -> XOR=0. That would be a move to a zero XOR position, which would be winning for the player who makes it? Actually, from a zero position, the next player loses. So if Alice moves to XOR=0, then Bob faces a losing position, so Alice would win. But is it possible for Alice to reduce the middle from 2 to 0? Yes, she can close both doors from either side. So why didn't we consider that as a winning move? Because we earlier said initial XOR=0 is losing for the player to move. But if Alice can move to XOR=0, then she would win, contradicting that initial is losing. Let's re-evaluate: In configuration "|__|", we said lengths (1,2,1). XOR = 1 xor 2 xor 1 = (1 xor 1) xor 2 = 0 xor 2 = 2? Wait, 1 xor 2 = 3, then 3 xor 1 = 2. Actually, 1 xor 2 = 3, 3 xor 1 = 2. So XOR=2, not 0. I mistakenly said XOR=0 earlier. Let's recalc: 1 xor 2 = 3, 3 xor 1 = 2. So XOR=2. So it's a winning position. Indeed, sample we had "||||" gave lengths 2 and 1 XOR=3. So I mis-copied. Let's find a zero XOR example: For three piles, need a xor b xor c = 0. E.g., 1,1,0 is zero but 0 is empty. So maybe 1,2,3 as earlier. So let's test "||___": lengths 1,2,3 -> XOR = 1 xor 2 = 3, 3 xor 3 = 0. So that is zero. So Alice should lose. Let's verify if Alice can move to a zero position? If she can, she would win. But if initial XOR=0, any move should change XOR to non-zero if our Grundy assignments are correct. Let's see if from (1,2,3) she can move to (0,2,3) -> XOR = 0 xor2 xor3 = 1 (non-zero). (1,0,3) -> 1 xor0 xor3 = 2. (1,2,0) -> 1 xor2 = 3. (1,2,1) -> 1 xor2 xor1=2. (1,2,2) -> 1 xor2 xor2=1. (1,2,3 cannot move to 1,2,3 with change? She can only reduce one pile. So from 3 she can reduce to 0,1,2. 0 gives XOR=1 xor2=3; 1 gives XOR=1 xor2 xor1=2; 2 gives XOR=1 xor2 xor2=1. All non-zero. From 2 can reduce to 0 or 1: 0 gives 1 xor3=2; 1 gives 1 xor1 xor3=3. From 1 can reduce to 0: gives 2 xor3=1. So indeed all moves lead to non-zero. So initial zero is losing. So consistent.
Thus, algorithm holds.
Now, need to consider if there is any nuance with the move: "He then selects a direction, either left or right, and starts to close the consecutive open doors in that direction." It says "starts to close the consecutive open doors". Does that mean he must close all consecutive open doors until he stops? Or can he skip? The description "He can stop whenever he wants or if he encounters a closed door." suggests he can stop at any point. So he can close any positive number up to the length of the open run.
Thus, we are good.
Now, let's think about the possibility that a move could be made from a closed door that is not at the immediate boundary of an open interval but still has an open door somewhere further away with a closed door in between? No, because the open doors must be consecutive starting from the immediate neighbor. So if there is a closed door in between, you cannot pass it.
Hence, our model stands.
Now, we should consider if there is any situation where the Grundy number of an open segment could be different due to the fact that the move might not be able to achieve all smaller sizes because of the requirement that the doors to be closed must be consecutive from the starting closed door. But we argued that from a segment of length L, you can achieve any length L' < L by closing exactly L - L' doors from one side. Since you can choose any number from 1 to L, you can achieve any new length from 0 to L-1. So yes.
But is it always possible to close exactly k doors from a given side without affecting the other side? Yes, because you stop after k. So you can choose any k.
Thus, Grundy = L.
Now, let's double-check the scenario where the open segment is at a boundary and the active side is the interior side. You can still close any number up to L. So fine.
Now, one more nuance: In a two-sided interval, could there be a move that reduces the length by taking from left or right, but resulting state is still (L - k, true, true). However, what if you close from left but the number you close is such that you reach the right closed door? That would be L - k = 0, which is allowed. So still same.
Thus, the impartial game for each open segment is exactly a Nim heap.
Now, we must consider whether the initial positions might have multiple closed doors adjacent to the same open segment? That's fine; it just gives two ways to reduce the heap, but the moves are the same set.
Now, what about the possibility that after some moves, an open segment might become adjacent to two closed blocks, but originally it might have been adjacent to only one? For instance, consider a string like "|_ _ |" but with a gap? Actually, starting with a single closed door and open on both sides, we have two separate open segments. If we close some doors from one side, the closed block expands, but the other open segment remains separate. So no new two-sided segments appear from one-sided ones? Actually, consider initial: "|". This has two one-sided segments (left and right). If Alice closes the left open door, the left segment disappears, and the closed block expands to include that door, so now we have only the right segment, which is still one-sided (since the closed block is now at left, and the right is boundary? Actually, after closing left door, the configuration becomes: "||"? Let's do: positions 1 '', 2 '|', 3 ''. After closing left door from closed at 2 leftwards, we close position1. So new: position1 '|', 2 '|', 3 '_'. So we have a closed block of two at positions 1-2, and an open segment at position3. That open segment is adjacent to closed block on left, and boundary on right? Actually, right side is boundary? If the string length is 3, position3 is the last, so right boundary. So it's a one-sided interval (left active). So indeed it remains one-sided.
Could a one-sided interval become two-sided later? For that to happen, a closed door would need to appear on the other side. But the other side is a boundary; boundary cannot become a closed door because doors are only within the line. So no. For an interior open interval, it might initially be one-sided if on one side there is a closed door and on the other side boundary? But interior intervals are never adjacent to boundary; they are between closed doors. So they are always two-sided if between two closed doors. Actually, consider a string: "|___". That's closed then open to the right, boundary at right. That's one-sided. It will never become two-sided because the right boundary remains boundary. So one-sided remains one-sided.
What about a string: "___|". That's open left, closed at right, boundary left: one-sided.
So flags are invariant.
Thus, our decomposition into independent piles with fixed Grundy = length is correct.
Now, is there any scenario where a move could affect two piles simultaneously? Suppose we have configuration: open segment A, closed door, open segment B. If we start at the closed door and close doors in one direction, we affect only that segment. So no.
Thus, the solution is straightforward.
Now, let's confirm with more examples via quick mental or programmatic simulation? Since we cannot run code here, we can reason manually for some tricky cases.
Consider a configuration with multiple closed doors and open segments: "||". That's: open2, closed1, open1, closed1, open3. Lengths: 2,1,3. XOR=2 xor1=3, 3 xor3=0 -> Bob wins. Let's see if Alice has a winning move. She can reduce any heap. Suppose she reduces the 2 to 0 (close both left doors) -> lengths: 0,1,3 XOR=1 xor3=2 (non-zero), Bob can then respond appropriately to get to zero. Or reduce 2 to 1 -> lengths:1,1,3 XOR=1 xor1 xor3=3 -> Bob can reduce 3 to 0? 3 xor 3 =0 -> reduce 3 to 0: then lengths 1,1,0 XOR=0. So Bob wins. If she reduces 1 to 0 -> lengths 2,0,3 XOR=2 xor3=1 -> Bob can reduce something to get zero? 1 xor? For example, reduce 3 to 2 (since 3 xor1 =2, and 2<3) -> lengths 2,0,2 XOR=0. So Bob wins. If she reduces 3 to 0 -> lengths 2,1,0 XOR=3 -> Bob can reduce 2 to 1 (since 2 xor3 =1) -> lengths 1,1,0 XOR=0. So Bob wins. So indeed losing.
Thus, the theory holds.
Now, we should think about the possibility of the game being misère Nim? In Nim, normal play (last move wins) where terminal position (no moves) is losing. Here, terminal is when no open doors exist (or no moves). That's analogous to all heaps zero. So normal Nim.
But is there any scenario where the terminal position is not all heaps zero? Suppose all open doors are gone but there are closed doors. That's terminal because no moves. So yes.
Thus, it's normal play Nim.
Now, we need to implement.
But we must be careful: The string length is ≤100, T up to 20000. So we can parse each string in O(N) time.
Algorithm:
for each test case:
s = input string
if '|' not in s:
print("Bob")
continue
xor_sum = 0
i = 0
while i < len(s):
if s[i] == '':
j = i
while j < len(s) and s[j] == '':
j += 1
length = j - i
xor_sum ^= length
i = j
else:
i += 1
print("Alice" if xor_sum != 0 else "Bob")
But wait: Is there any scenario where the presence of closed doors but an open segment that is not adjacent to any closed door? As argued, no.
But what about a string that contains only '_' and no '|'? That case we handled separately.
Now, let's consider a tricky case: Suppose the string is "|||" (ends with closed). That's open, closed, open, closed, open, closed? Actually, "|||" length 6: indices:1 '',2 '|',3 '',4 '|',5 '_',6 '|'. So open segments: [1] len1, [3] len1, [5] len1. Three piles of 1 -> XOR=1 -> Alice wins. That seems plausible.
Now, what about a string like "|||" (starts closed). That's closed, open, closed, open, closed. Open segments: [2] len1, [4] len1. XOR=0 -> Bob. Simulate: Alice can only close one of the open doors. Suppose she closes the left open door (position2) from closed at position1 right? Actually, closed at pos1 can go right to close pos2. Then state: positions:1 '|',2 '|',3 '|',4 '',5 '|'? Wait, need to check: initial: 1 '|',2 '',3 '|',4 '',5 '|'. After Alice closes door2 from closed at 1 right, we get: 1 '|',2 '|',3 '|',4 '',5 '|'. So now we have only open at 4, length1. Bob's turn: he closes it (from either closed at 3 or 5) and wins. If she instead closes door4, similar. So Bob wins. So correct.
Now, what about a string with multiple '_' and '|' such that some open segments are length 0? Not considered.
Thus, solution is clear.
But before finalizing, I want to examine the possibility of a move from a closed door that is not at the endpoint of a closed block but still has an open neighbor because the closed block has a "hole"? But holes would be open doors, so not closed block.
Thus, done.
Now, we should produce final answer with code.
But wait, there is also the constraint T up to 20000 and each string length ≤100, so our O(N) per test is fine.
One more check: The sample includes "|||" which yields Bob, our algorithm would find no '_' segments, XOR=0 -> Bob. Good.
Now, test edge: ""? Not possible as length at least 1? Not specified, but N is length of string, could be 0? Probably not, but we can handle empty string: no doors? Then no moves, Bob wins. But we can ignore.
Now, final answer.
But hold on! There is a nuance: The game description says: "In each turn, the person walks upto any closed door. He then selects a direction, either left or right, and starts to close the consecutive open doors in that direction. He can stop whenever he wants or if he encounters a closed door. However he must be able to close atleast one door. If he is unable to do so, he must concede defeat." This implies that the player must physically walk to a closed door. Could there be a situation where a closed door is not reachable because it's blocked by open doors? No, they can walk up to any closed door; presumably they can traverse open doors? The phrase "walks upto any closed door" suggests they can approach any closed door, maybe passing through open doors? But then they stand at that closed door. That seems fine.
Now, one might think that after closing some doors, the set of closed doors changes. But that doesn't affect.
Thus, answer is as above.
However, I recall a known problem from CodeChef maybe called "DOORS" or something. But I think the solution is indeed Nim.
Let's double-check by constructing a small game tree for a nontrivial configuration to ensure Grundy numbers are indeed lengths.
Consider a configuration: a single open interval of length 2 with two-sided (i.e., between two closed doors). According to our theory, g=2. Let's compute manually all positions and Grundy numbers for small lengths to confirm.
Define state as length L, with flags. But we can ignore flags for now since both sides active. Moves: can reduce to L=0 or L=1. So from L=2, moves to L=0 and L=1. Need Grundy of L=0: 0. L=1: from L=1, only move to L=0, so Grundy of L=1 is mex{0}=1. Then Grundy of L=2 is mex{0,1}=2. Yes.
Now, for one-sided interval length 2: same moves: to L=0 or L=1, so same Grundy = 2.
Thus, consistent.
Now, for length 3: moves to 0,1,2; Grundy of 0=0,1=1,2=2 -> mex{0,1,2}=3.
Thus, all good.
Now, we must ensure that from a one-sided interval, you can indeed move to any smaller length. For length 3, can you move to length 2? Yes, close 1 door from the active side. To length 1, close 2 doors. To 0, close 3. So yes.
Thus, solved.
Now, finalize.
But before finalizing, consider if there is any scenario where the XOR of lengths could be non-zero but the first player loses due to some structural constraint? That would imply our impartial sum is not correct, maybe because the games are not independent. Let's try to think of a potential interaction: Could a move in one interval affect the availability of moves in another interval? For example, consider two open intervals that share a closed block? Actually, they are separated by closed blocks. Suppose we have two open intervals A and B with a closed block between them. If a player closes doors from the closed block into A, the closed block expands into A, but does that affect the ability to attack B from that closed block? The closed block still has an endpoint adjacent to B (the other side) unchanged? Actually, if the closed block is of length >1, after closing into A, the closed block's endpoint on the A side moves, but the endpoint on the B side remains at the same position (assuming the closed block is contiguous). So B is still attackable from that side. So independent.
But what if the closed block is of length 1 initially (a single closed door). Then it is adjacent to both A and B. If a player closes from that closed door into A, then the closed door becomes part of a larger closed block that includes the newly closed doors. After the move, the configuration becomes: the original closed door plus the closed doors to the A side. Now, what about the B side? The original closed door is still there, but is it still adjacent to B? After closing into A, the original closed door is now interior to the closed block? Actually, if we start with a single closed door at position i, with A on left (positions < i) and B on right (positions > i). If we close m doors from the left (A), then we close positions i-1 down to i-m. After that, the closed block consists of positions i-m ... i (all closed). The rightmost closed door is at i (still), and the leftmost is at i-m. The door at i is still adjacent to B on its right (position i+1) because we haven't touched that side. So B is still adjacent to a closed door at i. So B remains attackable. Thus, independent.
Thus, no interaction.
Now, consider boundaries: If there is an open interval at left boundary, it can only be attacked from the right. If we attack that interval from the right, we extend the closed block leftwards. That doesn't affect other intervals.
Thus, independence holds.
Therefore, solution stands.
Now, we can write the code.
But we must note: The input format: first line contains T, then T lines each containing a string. The string can have spaces? No, it's just characters '_' and '|'. So we read normally.
Now, about output: print "Alice" or "Bob" each on separate line.
Thus, final answer.
But wait, there is one more nuance: The problem statement: "In each turn, the person walks upto any closed door. He then selects a direction, either left or right, and starts to close the consecutive open doors in that direction. He can stop whenever he wants or if he encounters a closed door. However he must be able to close atleast one door. If he is unable to do so, he must concede defeat." The phrase "He can stop whenever he wants or if he encounters a closed door." Might be interpreted as: when he encounters a closed door, he must stop (and he does not close that closed door). That matches.
Thus, final solution.
Now, let's write a more formal proof for the editorial: The game is equivalent to Nim with heap sizes equal to lengths of maximal open segments, as each such segment can be reduced by any positive amount from an adjacent closed door, and segments are independent.
Thus, code.
But before final, let's check some random cases with small N exhaustively (in mind) to ensure no counterexample.
We can try N=2:
Possible strings:
"__": no '|' => Bob. (Alice cannot move)
"_|": open1, closed1 => heap size1 -> Alice. Simulate: Alice closes the open door from the closed door (direction left) => all closed, Bob loses. Yes.
"|_": similar Alice.
"||": no open => Bob. Alice cannot move? She can pick a closed door but there are no open doors to close. So no move -> Bob.
N=3:
List all 2^3=8 strings:
"___": no | -> Bob. (Alice no move)
"__|": open2, closed1 => heap size2 -> Alice. Simulate: Alice can close 1 or 2 from the closed door leftwards. If she closes 2, all closed -> Bob loses. So Alice wins.
"|": two heaps of 1 -> XOR=0 -> Bob. Simulate: we already saw Bob wins.
"_||": open1, then closed2 => heap size1? Actually, open at pos1, then closed at pos2,3. So heap size1 -> Alice. She can close it -> all closed -> Bob loses. So Alice wins. XOR=1 -> Alice. Good.
All consistent with XOR of open segment lengths (with condition if at least one '|'? Actually, in all these with at least one '|', we used XOR of lengths. For "|", we have one open segment of length2, XOR=2 -> Alice. For "||", we have one open segment length1, XOR=1 -> Alice. For "||", length1 -> Alice. For "|_|", length1 -> Alice. For "|", length2 -> Alice. For "|", lengths 1 and 1 -> XOR=0 -> Bob. So matches.
"||_": This is length 5? Actually N=5 but we are at N=4. So skip.
All consistent.
Thus, solution is robust.
Now, we should consider if there is any scenario where the presence of multiple closed doors could cause an open segment to be "inactive" because the closed doors are not reachable? For example, if there is a closed door, but it is surrounded by closed doors, you cannot start at it because it's interior? But you could start at an endpoint. That endpoint is adjacent to an open segment. So it's fine.
Now, finalize.
But note: The problem might be from some contest and perhaps they intended a more complex solution? However, the sample matches the simple XOR.
Let's search memory: There is a known problem "Land of Doors" maybe from CodeChef or elsewhere. I recall a problem "Doors" where the answer was the XOR of lengths of continuous underscores. I think it's correct.
Nonetheless, we must ensure we haven't misinterpreted the direction of closing: "starts to close the consecutive open doors in that direction." Does he close the doors as he walks, meaning he also passes through them? He starts from the closed door and moves outward. He closes doors that are open. He can stop at any point. So he can close any number from 1 up to the number of consecutive open doors until a closed door or boundary.
Yes.
Now, about the requirement: "He must be able to close at least one door." So if from his chosen closed door in the chosen direction there is at least one open door before a closed door or boundary, he can close at least one.
Now, are there any restrictions that he cannot close doors that are not directly adjacent to the closed door if he skips? No.
Thus, done.
Now, I'll write the solution in Python (since T up to 20000, N <=100, Python is fine).
Pseudo:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
t = int(data[0])
out = []
idx = 1
for _ in range(t):
s = data[idx]; idx+=1
if '|' not in s:
out.append("Bob")
continue
xor = 0
i=0
n = len(s)
while i < n:
if s[i] == '':
j = i
while j < n and s[j] == '':
j+=1
xor ^= (j-i)
i = j
else:
i+=1
out.append("Alice" if xor != 0 else "Bob")
sys.stdout.write("\n".join(out))
But wait, what about strings that have no '_'? Then '|' not in s is false, we go to else; we compute xor over no segments -> xor=0 -> Bob. That's correct.
Now, confirm with all '' case: e.g., T=1, "___" -> no '|' -> Bob. Simulated: Alice cannot move, loses. Good.
Now, complexity: O(total length) which is ≤ 20000 * 100 = 2e6, fine.
Now, final answer.
But before finishing, let's think about potential misinterpretation: Could the game be misère Nim? In misère Nim, the winning condition flips when all heaps are of size 1. But here, the terminal position is when all heaps are zero, same as normal Nim. Could there be a scenario where a heap of size 1 is a terminal move? In Nim, if you take the last token from a heap, the heap disappears. Here, if you reduce a heap from 1 to 0, you close the last open door adjacent to that closed block. That is a valid move, and after that, the heap is gone. The game continues if there are other heaps. So it's exactly like Nim: taking the last token from a heap doesn't immediately end the game unless all heaps are zero. So normal play.
Thus, no misère nuance.
Now, let's consider if there is any possibility of a move that doesn't correspond to reducing a single heap, like starting at a closed door that is adjacent to two open intervals and closing doors in both directions? Not allowed.
Thus, solution final.
Now, I'll produce final answer with code and explanation.
But maybe we should consider the case where there is a closed door but the open segments are of zero length? Not relevant.
Thus, final.
Now, I'll write the answer in the required format. ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
t = int(data[0])
out_lines = []
idx = 1
for _ in range(t):
s = data[idx]
idx += 1
code
# If there is no closed door, Alice cannot move
if '|' not in s:
out_lines.append("Bob")
continue
xor_sum = 0
i = 0
n = len(s)
while i < n:
if s[i] == '_':
j = i
while j < n and s[j] == '_':
j += 1
length = j - i
xor_sum ^= length
i = j
else:
i += 1
out_lines.append("Alice" if xor_sum != 0 else "Bob")
sys.stdout.write("\n".join(out_lines))