We need to determine the winner of a game where two players alternately subtract the gcd(x, y) from both x and y. The player who makes at least one of the values zero (i.e., after subtraction, either x = 0 or y = 0 or both) wins. If after a move neither is zero, play continues.
Given initial (x, y) and who starts (Chandu or Daspal), determine the winner.
We need to compute the outcome for each test case.
Observations: The game is deterministic: given (x, y), the moves are forced because gcd(x, y) is uniquely determined. So the game sequence is completely determined. There's no choice. So it's just a matter of simulating the moves and counting who makes the last move.
But we need to be careful: the rule says: "During a move they are allowed to subtract gcd(x,y) from both x and y. The player which makes either one of the values to zero or both to zero will win the game." So after subtracting, if x'=0 or y'=0, that player wins immediately; else, the other player takes the turn.
So essentially, it's a game with no choices; the number of moves until termination is predetermined. The player who makes the move that results in a zero wins. So if the total number of moves (the move that makes a zero) is odd when counting from the first move (move 1 = first player), then the first player wins; else second.
So we just need to compute the number of steps (or parity) in the forced sequence.
But careful: The process is similar to the Euclidean algorithm but subtracting gcd from both numbers. Actually, what is the process? Starting with (x, y). Let g = gcd(x, y). Then new (x', y') = (x - g, y - g). This reduces both numbers by the gcd. But note: gcd(x, y) might change after subtraction. The Euclidean algorithm normally subtracts the smaller from the larger, but here we subtract the same amount from both. This is reminiscent of the "Euclid's game"? Actually, there is a known game: Starting with two positive integers, players alternately subtract a positive multiple of the smaller from the larger, but that's different. Here we subtract the gcd from both. Let's analyze.
We can attempt to simulate straightforwardly: for each test case, loop while both x and y > 0: compute g = gcd(x, y); subtract g from both; increment move count; check if either becomes zero. Since x and y ≤ 3000 and T ≤ 50, simulation is trivial; max steps? Let's see worst-case: The process of subtracting gcd from both might reduce numbers slowly? Possibly many steps? For example, starting with (1, 3000): gcd = 1, then (0, 2999) -> game ends in one move. That's fine. But what about numbers that are equal? (3000,3000): gcd=3000, subtract -> (0,0) in one move. So maximum steps might be when numbers are such that each subtraction reduces them by small amounts. Consider (2,3): steps: (2,3) -> (1,2) -> (0,1). 2 steps. (3,5): gcd=1 -> (2,4); gcd=2 -> (0,2) -> 2 steps? Actually (2,4): gcd=2, subtract -> (0,2) win. So two steps. (4,7): gcd=1 -> (3,6); gcd=3? Actually gcd(3,6)=3 -> (0,3) win -> 2 steps. (5,8): gcd=1 -> (4,7) -> gcd=1 -> (3,6) -> gcd=3 -> (0,3) win. That's 3 steps? Let's simulate:
Start (5,8): g=1 -> (4,7) (no zero)
Turn2: (4,7): g=1 -> (3,6) (no zero)
Turn3: (3,6): g=3 -> (0,3) win. So 3 steps.
So maybe the number of steps could be up to something like O(max(x,y)?) In worst-case, numbers could be consecutive Fibonacci? Possibly many steps like Euclid algorithm but each subtraction reduces numbers by gcd. Actually consider (1, n): one move. (2,3): 2 moves. (3,5): 2 moves. (5,8): 3 moves. (8,13): maybe? Let's compute (8,13): g=1 -> (7,12); (7,12): g=1? Actually gcd(7,12)=1 -> (6,11); (6,11): gcd=1 -> (5,10); (5,10): gcd=5 -> (0,5) win. That's 4 moves. So steps increase. Might be up to O(log(max))? Actually, similar to Euclidean algorithm but each step reduces one number by at least the gcd, but we subtract g from both. Note that after subtracting g, the difference (x-y) remains the same? Let's compute: (x - g) - (y - g) = x - y. So the difference d = |x-y| remains constant throughout the game! Because subtracting the same amount from both doesn't change the difference. Very important: d = |x - y| is invariant. Indeed, subtract g from both => difference remains same. So the absolute difference stays constant throughout the game. Let's denote d = |x - y|, which is constant.
Initially, we have x, y with d = |x-y|. And we subtract gcd(x, y) from both each move. Since gcd(x, y) divides both numbers, and also divides their difference? Actually gcd(x, y) divides any linear combination, so it divides d as well. So g divides d. Since d is constant and non-zero (if both positive? Could be zero initially if x=y; then d=0; game ends in one move because g=x, subtract yields (0,0)). If d = 0, then immediate win for first player (since they subtract x making both zero). So interesting: the process continues as long as both numbers positive and d > 0.
Now, given that difference d is constant, what is the effect of subtracting g? The numbers reduce but remain positive? Eventually, one of them will become zero when the smaller number becomes zero? Actually, after subtracting, the smaller number may become zero when it equals g exactly. Since we subtract g from both, if min(x, y) = g, then after subtraction that becomes 0, and the other becomes larger - g, which is >0 if d>0. So win condition: when min(x,y) = gcd(x,y). Actually, we need either x'=0 or y'=0. That occurs if x = g or y = g. Because after subtracting g, the new value is x - g. So if x = g, then x becomes 0. Similarly for y. Since g divides both, it's possible that both become zero if both equal g, i.e., x=y=g, which implies x=y, and d=0; but that's covered.
Thus, the game continues while min(x, y) > g. But g can change over time. The condition for termination at a move is that at the start of that move, the gcd equals the smaller number (or both numbers). Since the numbers are decreasing, eventually one will become equal to the gcd.
Because d is constant, we can analyze the process in terms of the pair (small, large) where large = small + d. Since both are positive and we subtract g from both, the new pair becomes (small - g, large - g) = (small - g, small + d - g) = (small - g, (small - g) + d). So the structure is preserved: after subtraction, the smaller number becomes small' = small - g, and the larger becomes small' + d. But careful: which is the smaller? If d > 0, after subtracting g, the ordering remains because both reduced by same amount, so the smaller remains the smaller, and the larger remains larger. Indeed, (small - g) and (small - g + d) have difference d. So the small is always the smaller number.
Thus, we can denote state as s = min(x,y) (the smaller value) and d = |x-y| > 0 constant. Actually, we can assume w.l.o.g. x <= y, then d = y - x. Then state is (x, d). And we have that y = x + d.
Now, what is gcd(x, y)? Since y = x + d, gcd(x, y) = gcd(x, x + d) = gcd(x, d). So g = gcd(x, d). That is interesting: gcd(x, y) = gcd(x, d) because gcd(x, x+d) = gcd(x, d). Also note that d is constant and >0. So g = gcd(x, d) depends only on the current smaller x and the constant d. So the move: subtract g from both => new smaller becomes x' = x - g, and d remains same.
Thus, the process reduces the smaller value x by g = gcd(x, d). And then the next state is (x', d) with same d. Continue until x becomes 0? Actually when x becomes zero? But win condition: after subtracting, if x - g = 0 (i.e., x = g) then that move wins. But also could y become 0 if y = g? But y = x + d. y = g would mean x + d = g, but since g <= x (since g divides x, but not necessarily less or equal? Actually g = gcd(x, d) divides x, so g <= x. Also d >0, so x + d > x >= g, so y = g only if x=0 and d=g? But x>0 during the game until termination; if x becomes 0 after subtraction, the move ends; but could y become 0 before x? For y to become 0, we need y - g = 0 => y = g. That implies x + d = g, but since x >= g? Actually g divides both x and d, but x can be less than g? No, if g divides x, then x >= g unless x=0. Since x>0, x>=g. So x + d >= g + d > g because d>0. So y cannot equal g while x>0. Therefore, the only way to get a zero after subtraction is when x = g, i.e., the smaller number becomes zero. So the game ends exactly when the smaller number becomes exactly equal to its gcd with d, i.e., when gcd(x, d) = x, meaning x divides d. Because if x divides d, then gcd(x, d) = x, since x|d. So termination condition: x | d, i.e., x is a divisor of d (and x <= d? Not necessarily; if x divides d, then d = kx, and since d > 0, k >= 1. But note that if x > d, can x divide d? Only if x <= d. For x > d, gcd(x, d) could be less than x, but also if x > d, can x divide d? No, because if x > d, the only way x divides d is if d=0, but d>0. So x must be <= d to possibly divide d? Actually if x > d, x cannot divide d (unless d=0). So termination occurs when x <= d and x divides d. But also initial x could be > d? Let's check: if x > d, then y = x + d > x, so indeed x is smaller, but x > d. Can x divide d? No, because x > d. So termination condition cannot be satisfied initially if x > d. So the process will continue until eventually x becomes <= d and divides d. At that point, the next move will cause x to become zero.
Thus, the game reduces to repeatedly subtracting g = gcd(x, d) from x, while d stays constant. The moves continue until x becomes zero, and the move that makes x zero wins.
Now, we need to compute the parity of the number of moves needed to reduce x to 0 via repeated subtraction of gcd(x, d). Since each move reduces x by g, where g = gcd(x, d). Note that after subtraction, x' = x - g. The new g' = gcd(x', d). This process is akin to: starting from initial x0 (the smaller of initial pair), with constant d. At each step, we subtract the current gcd(x, d) from x. This continues until x becomes 0. This is deterministic.
We need to determine if the number of steps is odd or even (depending on who starts) to decide winner.
Since T up to 50 and x,y up to 3000, simulation is trivial. Even in worst-case, how many steps could there be? The process reduces x by at least 1 each move, but sometimes by more. In worst-case scenario, d = 1. Then gcd(x, 1) = 1 always. So each move subtracts 1 from x. Starting from x (the smaller number) which could be up to 3000 (if y = 3001? But y ≤ 3000, so max x is 3000, d could be 0? Actually if d=1, then y = x+1, with x up to 2999? Since y ≤3000, x can be 2999, d=1. So steps = x? Because we subtract 1 each time until x becomes 0? Let's simulate with d=1: start (x, x+1). At each move, g=1, so subtract 1 from both: new x becomes x-1, d remains 1. Continue until x becomes 1? Let's check termination condition: x divides d? d=1, so when x=1, gcd(1,1)=1, so x=1 divides d (1 divides 1). Then next move subtracts 1, making x=0. So number of moves = initial x? Because we need to reduce x from initial value to 0 by subtracting 1 each move. Starting from x0, after first move, x1 = x0 - 1; after k moves, x_k = x0 - k. The game ends when after a move we get x_k = 0; i.e., when x0 - k = 0? Actually careful: The move that makes x zero: we subtract g from current x to get zero, i.e., x - g = 0 => x = g. So the move is performed when current x = g. So the number of moves is the number of steps until we reach a state where x divides d, and then we perform one more move? Let's simulate with d=1, x0=3: State1: (3,4). g=gcd(3,1)=1, subtract => (2,3). No zero. Move1. State2: (2,3). g=gcd(2,1)=1, subtract => (1,2). No zero. Move2. State3: (1,2). g=gcd(1,1)=1, subtract => (0,1). Now zero, win. So total moves = 3. Initial x0=3 => moves = 3. So indeed moves = initial x0 (since each move reduces x by 1, and we need to reduce from 3 to 0 inclusive? Actually moves count = 3). For x0=1: State (1,2). g=1 subtract => (0,1). One move. So moves = x0. So for d=1, number of moves = x0 = min(x,y). That could be up to 2999? Actually if y = 3000, x can be as low as 1; but max x0 = ? The larger the x0, the larger the moves. Since y ≤ 3000, the maximum possible min(x,y) could be 1500? Not exactly; the max min is when numbers are equal? If x=y, then d=0, moves=1. So that's not large. To maximize min while keeping d>0, we want them both close to 3000 but with a small difference. For example, x=2999, y=3000, then min=2999, d=1. So moves=2999. So steps could be up to 2999, which is fine for simulation. T up to 50, so worst-case total steps ~ 50*3000 = 150k, trivial.
Thus, we can just simulate the game straightforwardly: while true: compute g = gcd(x, y); subtract from both; check if either becomes 0; if so, winner = current player; else switch player.
We need to take care: The given input includes a string for who starts. So we can simulate.
But maybe there is a more efficient mathematical approach, but not needed.
Given constraints, simulation is perfectly acceptable.
But let's double-check: Are there any edge cases? x,y up to 3000, both positive. The gcd computation can be done with built-in function or Euclid.
One subtlety: The rule says "The player which makes either one of the values to zero or both to zero will win the game." So after a move, if either x or y becomes zero, that player wins. So we check after subtraction.
We also need to handle the case where initial (x,y) already has a zero? But constraints say 1 ≤ x,y, so both are positive initially.
Also need to handle the case where after subtraction both become zero? That's when x = y = g, i.e., x=y initially. Then first player wins. Because subtract gcd(x,x)=x, both become zero.
Simulation steps:
For each test case:
- read x, y, starter (string).
- set current_player = starter.
- while True:
- compute g = gcd(x, y)
- x -= g; y -= g
- if x == 0 or y == 0:
- winner = current_player
- break
- else:
- switch current_player (if Chandu then Daspal, else Chandu)
Output winner.
Now, is there any scenario where the game might loop infinitely? Since both numbers are positive and each move reduces both by at least 1 (since gcd >= 1), they will eventually become non-positive. Since we only subtract g from both, and we stop when either becomes zero. They cannot become negative because if one becomes zero, the move stops and wins. Could it become negative? Suppose x < g? But g = gcd(x, y) and g divides both, so g <= min(x, y). So min(x,y) >= g, so after subtraction min(x,y)-g >= 0. So they never become negative.
So simulation will terminate.
But we need to check if the condition "either one of the values to zero or both to zero" is considered immediately after subtraction. In our simulation, we subtract, then check. That's correct.
Edge case: If after subtraction both become zero, that's also captured.
Now, the sample input: 2 3 Chandu -> we simulate:
Start: (2,3), player=Chandu, g=gcd=1, -> (1,2), no zero -> switch to Daspal.
(1,2), player=Daspal, g=gcd=1, -> (0,1) -> zero -> Daspal wins. Output Daspal. matches.
So simulation is straightforward.
But maybe there's a trick: The rule says "If neither of values after 1st step does not equal to zero then the second player gets a chance and play as described by rule 1,2,3." So after first step if not zero, second player gets chance. So indeed alternating.
Thus, answer is simulation.
Given T up to 50, x,y up to 3000, simulation is fine.
But wait: Could there be cases where the number of steps is extremely large? For d=1, steps = min(x,y). That's at most 2999, which is fine. But is there any scenario where the steps exceed 3000? Let's think: Suppose d=2, and initial x is odd maybe? gcd(x,2) could be 1 or 2. If x is odd, gcd=1, subtract 1 => x becomes even? Actually if x odd, x-1 becomes even. Then later gcd may be 2, subtract larger. Steps likely less than or equal to x? Since each step reduces x by at least 1, total steps <= x. Maximum x can be 3000? Actually with d small, x can be up to 3000? But y = x+d must be <=3000, so x <= 3000 - d, so x max is 3000-d. So x <= 2999 for d>=1. So steps <= 2999. So simulation is okay.
Thus, solution: implement gcd, simulate.
But maybe we can derive a closed form. For fun: Since d constant, the process essentially reduces x by gcd(x,d) repeatedly. This is analogous to the process of repeatedly subtracting the gcd of the current number and a fixed number d. This might correspond to the number of steps being something like the number of times we need to apply this to reduce x to 0. In particular, the process is deterministic. For d=1, steps = x. For d>1, it's less. Possibly we could compute number of steps quickly using some formula, but not needed.
Nevertheless, we should ensure that our simulation is correct for all cases. Let's test a few more:
Case: (1,1) with Chandu: gcd=1, subtract -> (0,0), winner Chandu. Steps=1.
Case: (2,2) Chandu: gcd=2 -> (0,0), winner Chandu.
Case: (3,3) similar.
Case: (3,6) Chandu: Since x=3,y=6, d=3, g=gcd=3, subtract -> (0,3) => zero immediate, winner Chandu. So one move.
Case: (4,6) Chandu: min=4, d=2? Actually if we take x<=y: x=4,y=6, d=2. g=gcd(4,6)=2, subtract -> (2,4) not zero. Switch to Daspal. Now state (2,4) with d still 2? Actually difference 2. g=gcd(2,4)=2, subtract -> (0,2) zero, Daspal wins. So 2 moves => second player wins. Simulation: Starting Chandu, after first move no zero, then Daspal wins. So output Daspal.
Check via parity: steps = 2 => second player wins.
Case: (5,7) Chandu: min=5, d=2. gcd(5,7)=1, subtract -> (4,6); not zero, switch. (4,6): gcd=2, subtract -> (2,4); not zero? Wait, after subtract (4,6) becomes (2,4) which no zero. But after that, next move: (2,4) gcd=2, subtract -> (0,2) zero. So total moves = 3? Let's simulate:
Start: (5,7) player=Chandu, g=1 -> (4,6) no zero.
Player Daspal: (4,6) g=2 -> (2,4) no zero.
Player Chandu: (2,4) g=2 -> (0,2) zero -> Chandu wins.
So winner Chandu (first player) if starting with Chandu. So steps = 3 (odd) => first player wins.
Thus, simulation works.
Now, consider (10,15): min=10,d=5. gcd(10,15)=5, subtract -> (5,10) no zero? Actually (5,10) has one zero? No. After subtraction: (5,10). Then Daspal: gcd(5,10)=5, subtract -> (0,5) zero, Daspal wins. So steps = 2 => second player wins.
(7,10): min=7,d=3. gcd(7,10)=1 -> (6,9) not zero; next: (6,9) gcd=3 -> (3,6) not zero; next: (3,6) gcd=3 -> (0,3) zero. So steps =3: first player wins.
Thus, pattern: number of steps = number of times we need to subtract until x becomes zero. That's basically the number of "gcd reductions" needed.
Now, implementation details:
Read T.
For each test case: input: we have two integers and a string. Format: "x y Name". For example: "2 3 Chandu". There is no extra spaces? Probably the string is after a space. So we can read with input().split(): first two as ints, third as string.
We'll simulate:
def gcd(a,b):
while b: a,b = b, a%b
return a
But careful: when we compute gcd(x,y) each iteration, both positive.
We'll loop:
turn = 0 (0 for first player, 1 for second maybe)
But simpler: current = starter (string). Then:
while True:
g = gcd(x,y)
x -= g
y -= g
if x == 0 or y == 0:
winner = current
break
else:
if current == "Chandu":
current = "Daspal"
else:
current = "Chand"
But note: string exactly "Chandu" and "Daspal"? Input: "Chandu" and "Daspal". Must match case? As given.
So we can use if current == "Chandu": current = "Daspal" else: current = "Chandu".
Edge: After subtraction, if both become zero, condition holds.
Now, potential optimization: we could compute steps without simulation using math, but not needed.
But wait: The problem says "During a move they are allowed to subtract gcd(x,y) from both x and y." It doesn't say they must subtract exactly gcd(x,y), but they are allowed to. The phrasing "allowed to subtract gcd(x,y)" implies that's the only allowed subtraction? It says "they are allowed to subtract gcd(x,y) from both x and y." Could there be any alternative? Typically in game theory, "allowed to subtract" might imply they can subtract any positive multiple? But the phrasing is ambiguous. However, the sample explanation uses exactly gcd(2,3)=1 and subtracts 1, then gcd(1,2)=1 and subtracts 1. So it's forced to subtract the gcd. Also rule 2 says "The player which makes either one of the values to zero or both to zero will win the game." So there's no choice; the move is determined. So it's a deterministic game.
Thus, our interpretation is correct.
But let's consider if there might be a nuance: "During a move they are allowed to subtract gcd(x,y) from both x and y." Could be interpreted as they can subtract any number that is the gcd of some pair? But no, it says "subtract gcd(x,y)" - the current gcd. So it's forced.
Thus, simulation is fine.
Now, about the constraints: T up to 50, x,y up to 3000. Simulation worst-case for each test: up to ~3000 iterations each with gcd calculation (which is O(log min) but min small). So it's fine.
We'll implement.
But wait: In the problem statement they gave sample input: "1\n2 3 Chandu". That's simple. So code should output "Daspal".
Let's think about potential pitfalls: Input might have extra whitespace or newline. We'll just use split.
But note: The string may be "Chandu" or "Daspal". Might have uppercase? Usually exactly as given: "Chandu" and "Daspal". So we'll compare as is.
Also, there might be cases where the input has "Chandu" and "Daspal" with correct capitalization. Output must match exactly.
Now, we might try to derive a direct formula: The number of steps is equal to (min(x,y) / g0?) Not necessarily. Actually, note that the process can be seen as: starting from (x, y) with d constant, we are essentially performing something like: while x > 0: x <- x - gcd(x, d). This is like repeatedly applying a function. Could we compute number of steps as sum of something? But not needed.
We need to consider that sometimes the first player could be "Daspal". So we need to output accordingly.
Alright, simple simulation will pass.
But wait, could there be a scenario where the game never ends? No, because each move reduces x by at least 1 (since gcd(x,d) >= 1). x is positive integer, so after at most x steps, x becomes 0? But careful: the process stops when x becomes 0 after a subtraction. But if we subtract g, x becomes x - g. Since g >= 1, after at most x steps, x will become 0. But note that g may become larger than x? No, g <= x. So indeed, after at most x steps (each subtracting at least 1), we will reach 0. Actually the number of steps could be exactly x only if g=1 each time. So bounded by initial min(x,y). So safe.
Thus, code.
We'll implement in Python.
Potential improvement: Use iterative method:
def steps(x, y):
# ensure x <= y
if x > y: x, y = y, x
d = y - x
moves = 0
while x > 0:
g = math.gcd(x, d) # since gcd(x, y) = gcd(x, d)
# But careful: If d = 0, then we are done immediately? Actually if d=0, then the game ends in one move because x = y, so g = x, subtract -> (0,0). So handle separately.
if d == 0:
return 1 # but we should treat separately.
# Subtract g from x; but we need to check if after subtraction x becomes 0? Actually we count move here.
# We'll increment moves, then set x = x - g.
moves += 1
if x == g: # after this subtraction we would get 0, and game ends.
break
x -= g
return moves
But careful: In simulation, we stop when after subtraction we get zero. In the above, we increment moves and then check if x == g (i.e., before subtraction? Actually before subtraction, x is current. The move subtracts g; if x == g, then after subtraction x becomes 0. So we would do moves++, and then break; we shouldn't subtract further. So we can either simulate exactly.
But using the difference approach could be faster, but simulation is fine.
Given that the problem likely expects an efficient solution maybe? But with constraints 3000, it's fine.
However, let's double-check sample: (2,3): x=2, d=1. Steps? While x>0: g = gcd(2,1)=1; moves=1; if x==g? 2==1 false; x=2-1=1. Next: g=gcd(1,1)=1; moves=2; if x==g? 1==1 true; break. Moves=2. So parity even => second player wins if first starts. Good.
For (5,7): x=5,d=2: moves: g=1 (5,2 gcd=1) -> moves=1, x=4; g=gcd(4,2)=2 -> moves=2, x==g? 4==2 false; x=2; g=gcd(2,2)=2 -> moves=3, x==g? true break. Moves=3 odd => first player wins.
So this method works.
Edge case d=0: Then initially x=y. We can just say moves=1.
Thus we could compute moves in O(initial x) time. But simulation of (x,y) directly also O(initial min). So similar.
Either is fine.
Given T small, either is acceptable.
Thus, answer: implement simulation.
Now, I'll produce final code with a function to compute gcd.
But need to be careful about reading input: The test cases: "2 3 Chandu". Some lines might have trailing spaces. Use split.
Also, after reading T, we may need to handle that the string might be on same line as numbers. Use split().
Pseudo:
import sys, math
def main():
T = int(sys.stdin.readline())
for _ in range(T):
parts = sys.stdin.readline().split()
x = int(parts[0])
y = int(parts[1])
starter = parts[2]
# simulation
cur = starter
while True:
g = math.gcd(x, y)
x -= g
y -= g
if x == 0 or y == 0:
winner = cur
break
# switch player
if cur == "Chandu":
cur = "Daspal"
else:
cur = "Chandu"
print(winner)
if name == "main":
main()
This should work.
But we need to be careful about the possibility that after subtraction both become zero. That condition is covered by x==0 or y==0, since both zero means both are zero.
One more thing: In Python, math.gcd works for positive ints.
Now, we could also use the difference method for speed, but not necessary.
Now, consider if the starter is "Daspal". Then after simulation, we output winner accordingly.
Test with some cases.
Let's manually test (1,1 Chandu) -> g=1, subtract -> (0,0), winner Chandu.
(1,2 Daspal) -> start Daspal: g=1 subtract -> (0,1) -> zero -> Daspal wins. So output Daspal.
(2,2 Daspal) -> Daspal wins.
(3,5 Chandu) -> we determined moves=3, so Chandu wins.
Let's simulate with code: (3,5) Chandu:
cur=Chandu
g=gcd(3,5)=1 => (2,4) no zero -> switch to Daspal
g=gcd(2,4)=2 => (0,2) zero -> winner Daspal? Wait, that gives Daspal, but we earlier thought moves=3? Let's recalc: Starting (3,5):
- Move1 (Chandu): g=1 -> (2,4) (neither zero)
- Move2 (Daspal): g=2 -> (0,2) (zero) -> Daspal wins. That is only 2 moves. But earlier we said moves=3. Let's recalc earlier example: I think I made a mistake earlier. For (3,5), is d=2? Because min=3, y=5, d=2. Then g1 = gcd(3,2)=1 -> subtract => (2,4). Now (2,4): min=2, d=2, g2 = gcd(2,2)=2 -> subtract => (0,2). That's two moves. So winner Daspal if Chandu starts. But earlier I thought (5,7) gave 3 moves. Let's recalc (5,7): min=5, d=2. g1 = gcd(5,2)=1 -> (4,6). (4,6): min=4, d=2, g2 = gcd(4,2)=2 -> (2,4). (2,4): min=2, d=2, g3 = gcd(2,2)=2 -> (0,2). So that's 3 moves. So (3,5) took 2 moves because after first move (2,4) and then g=2 leads to termination. So parity depends on the specifics.
Thus, code simulation yields correct.
Now, what about (5,7) Chandu: as per simulation:
cur=Chandu: g=gcd(5,7)=1 -> (4,6) no zero -> switch to Daspal.
cur=Daspal: g=gcd(4,6)=2 -> (2,4) no zero? After subtraction (4-2,6-2) = (2,4). Both >0, so no zero -> switch to Chandu.
cur=Chand: g=gcd(2,4)=2 -> (0,2) zero -> winner Chandu. So Chandu wins.
Matches.
Thus, code works.
Now, edge: (1,3000) Chandu: g=1 -> (0,2999) -> Chandu wins.
(2999,3000) Chandu: g=1 -> (2998,2999) not zero -> switch -> ... many steps, final winner depends on parity of 2999 steps? Let's compute moves using difference method: x0=2999, d=1 => moves = x0 = 2999. Since 2999 is odd, first player wins. So if Chandu starts, winner Chandu.
Check via simulation concept: For d=1, each move reduces x by 1, number of moves = initial x. Since initial x=2999, odd => first player wins. So output Chandu.
Now, (2998,3000) maybe? That's even? But ensure min is 2998, d=2? Actually 3000-2998=2, so d=2. Then moves may be less. But anyway.
Thus, all good.
Now, think about if the initial pair not sorted: we don't need to sort because gcd(x,y) symmetric and subtraction symmetrical. However, the condition for zero: if after subtraction either x or y becomes zero, winner. If we don't sort, simulation works. But we must ensure that gcd(x,y) is computed correctly. So no need to sort.
But careful: If we keep the numbers unsorted, the condition that x becomes zero may happen earlier than y. It's fine.
Thus, simulation code as above is correct.
Now, check for possible infinite loop if we mis-handle condition. For example, if after subtraction both are zero, we break.
Now, produce final answer.
But wait, there is potential to misinterpret rule 2: "The player which makes either one of the values to zero or both to zero will win the game." This is after subtraction. So if a player makes a move that results in zero, they win. So simulation correct.
Thus, final code is straightforward.
But I'll also present an alternative mathematical solution for completeness, but not necessary.
Given this is CodeXplod series, maybe they expect to use the invariant property and compute moves quickly. However, with constraints 3000, simulation is okay.
I'll produce final answer with simulation.
But before final, test with random small numbers to ensure parity aligns with a possible formula.
Let's brute small numbers manually or think about pattern:
We can compute number of steps as a function of (x, y) with x<=y, d=y-x.
Define f(x, d) = number of moves until termination starting with smaller x and constant d.
Recurrence: If x divides d? Actually termination condition: if x divides d, then next move (the one we are about to make) will cause x to become zero? Wait careful: f(x, d) counts the number of moves required to end the game. That includes the move that results in zero. So if x divides d, then g = gcd(x, d) = x, so after subtracting, x becomes 0. So the game ends in one move. So f(x, d) = 1 if x divides d.
Otherwise, f(x, d) = 1 + f(x - g, d), where g = gcd(x, d).
Because we make one move (subtracting g) and then continue with new smaller = x - g.
Thus, we can compute recursively. That's essentially simulation.
Thus, solution stands.
Potential optimization: Precompute f for all x up to 3000 and all possible d up to 3000? But d is up to 2999. That's 3000*3000 ~ 9e6, still doable but not needed.
Thus, simulation per test case is fine.
One more nuance: The problem may be part of a contest where time limit is strict, but Python with T=50 and max steps 3000 each is definitely fine.
Thus, final answer: implement simulation.
Now, write final solution with proper reading and output.
I'll produce code with function solve().
Note: Use math.gcd or implement.
Edge case: Input may have multiple spaces or newline after T. Use input().strip().
Also, after reading T, there might be blank lines? Unlikely.
Thus, final code:
import sys
import math
def solve():
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
T = int(next(it))
out = []
for _ in range(T):
x = int(next(it))
y = int(next(it))
starter = next(it)
cur = starter
while True:
g = math.gcd(x, y)
x -= g
y -= g
if x == 0 or y == 0:
winner = cur
break
cur = "Daspal" if cur == "Chandu" else "Chandu"
out.append(winner)
sys.stdout.write("\n".join(out))
if name == "main":
solve()
This reads all at once to avoid per-line overhead.
Check: For sample input "1\n2 3 Chandu", data = ['1','2','3','Chandu']; T=1; then x=2,y=3,starter='Chandu'; simulation yields winner='Daspal'; out = ['Daspal']; print -> "Daspal". Good.
Thus, final answer.
But let's double-check with a case where starter is Daspal: e.g., "1\n1 2 Daspal". Should output "Daspal". We'll test quickly mentally: cur=Daspal, g=1 -> (0,1) -> winner Daspal. Works.
Edge: T=0? Not allowed.
All good.
Thus, answer is ready. ```python
import sys
import math
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
T = int(next(it))
results = []
for _ in range(T):
x = int(next(it))
y = int(next(it))
starter = next(it) # "Chandu" or "Daspal"
current = starter
while True:
g = math.gcd(x, y)
x -= g
y -= g
if x == 0 or y == 0:
winner = current
break
# switch player
current = "Daspal" if current == "Chand" else "Chand"
results.append(winner)
sys.stdout.write("\n".join(results))
if name == "main":
solve()