We need to find the maximum total stones Alice can take, given operations:
- Op1: take 1 from heap1, 2 from heap2. So total stones taken = 3, but it consumes resources.
- Op2: take 1 from heap2, 2 from heap3. Total stones taken = 3.
She can perform these operations any number of times, as long as constraints hold.
Goal: maximize total stones taken (sum of stones taken from all heaps). Since each operation takes exactly 3 stones total, maximizing number of operations is equivalent to maximizing total stones? But not exactly, because operations are of type (1,2) or (2,1) essentially. Actually each operation takes 3 stones, so total stones taken = 3 * (number of operations). So maximizing total stones is equivalent to maximizing the number of operations, as long as each operation consumes stones appropriately.
But note: the operations consume stones from the heaps, and we need to find a sequence of operations (possibly mixing both types) that maximizes total number of operations, given initial counts a, b, c.
Thus problem reduces to: maximize x + y, where x = # of op1, y = # of op2, subject to constraints:
- For op1: each consumes 1 from heap1 and 2 from heap2. So total from heap1: x ≤ a. Total from heap2: 2x + y ≤ b? Wait: op1 consumes 2 from heap2, op2 consumes 1 from heap2. So heap2 consumption: 2x + y ≤ b.
- For op2: each consumes 1 from heap2 and 2 from heap3. So heap3 consumption: 2y ≤ c? Actually op2 consumes 2 from heap3 per operation, so total heap3 used: 2y ≤ c.
- Also op1: heap1: x ≤ a.
- Nonnegativity: x, y >= 0 integers.
But also note the operations have preconditions: for op1, need at least 1 in heap1 and at least 2 in heap2. But if we treat constraints as total consumption, we must ensure that at each step we don't run out. However, if we just care about totals, any sequence that satisfies total consumption ≤ initial counts is feasible? Possibly not always, because the order matters if we interleave operations: e.g., you might need to use heap2 for both, but if you use too many op2 first, you might not have enough heap2 left for op1's required 2 per operation. But since operations only consume, and we can reorder? Typically, if there exists non-negative integers x,y satisfying constraints, there is a feasible sequence? Need to check: Suppose we want to do x op1 and y op2. The total requirements: heap1: x; heap2: 2x+y; heap3: 2y. If we have enough total, can we always find an order? Possibly yes, because we can schedule operations so that we never violate the instantaneous constraints. For example, we could do all op1 first: need heap1 and heap2. As long as we have enough heap2 initially, we can do x op1. Then after op1, we have leftover heap2 = b - 2x, heap3 = c, heap1 = 0. Then we can do y op2 as long as b - 2x ≥ y? Actually op2 requires 1 from heap2 and 2 from heap3 per operation. After op1, we have heap2' = b - 2x, heap3 = c. Then we need y ≤ heap2' and 2y ≤ c. So that imposes y ≤ b - 2x. But in our total constraints, we only required 2x + y ≤ b, which is equivalent to y ≤ b - 2x, so that condition is satisfied. Also 2y ≤ c. So doing all op1 first works. However, what if we did op2 first? Then we would need heap2 >= y, heap3 >= 2y. After that, heap2'' = b - y, heap1 = a, heap3' = c - 2y. Then we need x ≤ a, and also 2x ≤ heap2'' = b - y? Actually op1 requires 2 from heap2, so need 2x ≤ b - y, i.e., 2x + y ≤ b, which is the same. So either order works if total constraints hold. But we must ensure at each step the instantaneous condition holds. Is it always possible? For some (x,y) satisfying total constraints, maybe no ordering exists because the requirement for op1 is that at the time of doing op1, there are at least 2 heap2. If we interleave, it's fine. But let's test extreme: a=0, b=2, c=2. Then total constraints: x ≤ 0, so x=0; then 2*0+y ≤2 => y≤2; 2y≤2 => y≤1; so max y=1. Can we do 1 op2? Yes. So okay.
Another scenario: a=1, b=3, c=0. Then op2 impossible because c=0, so only op1 possible. Total constraints: x≤1, 2x+ y ≤3, 2y ≤0 => y=0. Then x≤1, 2x≤3 => x≤1. So max x=1, works: do op1 first. Works.
Edge: a=1, b=2, c=100. Total constraints: x≤1, 2x+y≤2, 2y≤100. So from 2x+y≤2 with x≤1, possibilities: x=0 gives y≤2; x=1 gives y≤0. So we can have (x=0,y=2) or (x=1,y=0). Which yields total operations 2 vs 1. (x=0,y=2) gives total stones =6, (x=1,y=0) gives 3. So max is 6. But can we perform 2 op2? Need to perform op2 twice. Each op2 requires 1 from heap2 and 2 from heap3. Initially heap2=2, heap3=100. After first op2, heap2 becomes 1, heap3=98. After second op2, heap2 becomes 0, heap3=96. So it's fine. So works.
Another edge: a=1, b=4, c=1. Then total constraints: x≤1, 2x+y≤4, 2y≤1 => y=0. So x=1, total=3. That seems fine.
But consider a=2, b=5, c=0. Only op1 possible. x≤2, 2x≤5 => x≤2. So x=2, total=6. Need to do 2 op1. Requires each time heap2>=2. After first op1, heap1 becomes 1, heap2 becomes 3. Then second op1 requires heap1>=1 and heap2>=2, which holds. So works.
Now consider a=1, b=3, c=1. Total constraints: x≤1, 2x+y≤3, 2y≤1 => y=0. So x=1 total=3. Alternatively could we do op2 once? y=1 would need 2y≤1 => no. So only x=1 works.
Now consider a=0, b=2, c=2 we already did.
Now consider a=2, b=1, c=4. Then total constraints: x≤2, 2x+y≤1 => Since 2x≥0, the max x can be 0 if 2x >1 then impossible. Actually 2x+y≤1 with x integer forces x=0 and y≤1; and 2y≤4 => y≤2, but y≤1 from b, so y=1. So total=3. Could we do op1? No. op2 once: possible. Works.
Now, is there any case where total constraints are satisfied but no ordering? Suppose we try to do both types; we need to ensure that when we interleave we don't get stuck. But we can always do all op1 first (if x>0) then op2, provided that after op1, heap2 left (b-2x) >= y. That's exactly condition 2x+y≤b, which is part of total constraints. Also op1 requires heap1 which is fine as we have a≥x. Op2 then requires heap3≥2y, which is also part of total constraints. So all x,y satisfying the constraints can be realized by performing all op1 first, then all op2. That's a valid sequence because after op1, we still meet the preconditions for op2? For op2, we need at least 1 heap2 and at least 2 heap3. Since after op1 we have heap2' = b-2x, which is ≥ y≥? Actually we need at least 1 per op2. But if y>0 and heap2' >= y, then heap2' is at least 1 for each operation, but we do them sequentially: first op2 requires heap2'>=1, after that heap2'' = heap2' -1, still >=0. If y = heap2', that's fine as long as heap2'>=1 for each operation; but if y == heap2' and heap2'>0, then we can do them all because each op consumes 1 from heap2. For the last op, heap2 must be at least 1, which is true if heap2' >= y. Actually if heap2' = y, then before the last op, heap2 after previous ops is 1 (since we have done y-1 ops, consumed y-1, leaving heap2' - (y-1) = y - (y-1) = 1). So that's fine. Similarly for heap3: we need at least 2 per op2. Starting with heap3=c, need c >= 2y. After each op, heap3 decreases by 2. So as long as c >= 2y, we can do them. So indeed all good.
If x=0, then doing op2 first is fine; if y=0, op1 first fine.
Thus the problem reduces to integer linear programming: maximize x+y subject to:
x ≤ a
2x + y ≤ b
2y ≤ c
x, y ≥ 0 integers.
We can brute force over possible x (0..a) and compute max y satisfying both constraints: y ≤ b - 2x, y ≤ floor(c/2). Then answer = 3*(x + min(b-2x, floor(c/2))? Actually y = min(b - 2x, floor(c/2)). So total = x + min(b-2x, floor(c/2)). Then find x that maximizes that. Since a,b,c ≤ 100, we can simply loop over x from 0 to a and compute, time O(100) per test case. That's fine.
But we should also consider the possibility of doing operations in a different order that might allow more operations than the linear constraints suggest? For example, what about alternating? But we argued that if total constraints are satisfied, a sequence exists. Could there be sequences that exceed the total constraints? No, because any operation consumes stones, so the total consumption of each heap cannot exceed initial amounts. So x ≤ a, 2x + y ≤ b, and 2y ≤ c are necessary conditions. They are also sufficient as we demonstrated by ordering. So the maximum number of operations is the maximum of x + y subject to those constraints.
Thus answer = 3 * max_{x=0..a} [ x + min( floor(c/2), b - 2x ) ], with the note that b - 2x must be non-negative, so x ≤ floor(b/2). Also y must be integer >=0.
We could also consider symmetrical reasoning swapping heaps? But no.
Alternatively, we could think in terms of balancing: Since each operation uses two stones from one heap and one from another, there might be another approach like greedy. But the brute force over small range works.
Alternatively, we can solve analytically: For each possible y, x ≤ min(a, floor((b - y)/2)). Then total = y + min(a, floor((b - y)/2)). Then maximize over y from 0 to floor(c/2) and also y ≤ b. Equivalent. But loop over x is fine.
Given constraints t up to 100, we can implement easily.
Check examples:
Example1: a=3,b=4,c=5.
Loop x:
x=0: y=min(floor(5/2)=2, b-2x=4) -> y=2 => total=2.
x=1: y=min(2,4-2=2) -> y=2 => total=3.
x=2: y=min(2,4-4=0) -> y=0 => total=2.
x=3: y=min(2,4-6=-2 -> negative, so 0?) Actually b-2x = -2 <0 => min with non-negative? Actually y cannot be negative; we must set y=0 if b-2x <0? But min(2, -2) would be -2, but we need y non-negative. So we need to compute y = max(0, min(floor(c/2), b-2x) if b-2x >=0 else 0). So better: if b - 2x < 0, then y=0 because no y can be positive if heap2 insufficient? Actually total constraint 2x + y ≤ b; if b-2x is negative, then the only possibility is y=0? But then 2x ≤ b? If b-2x <0, then 2x > b, meaning x is too large to satisfy even without y. But also x ≤ a but also 2x ≤ b? Actually we need 2x ≤ b for any feasible x (since y≥0). So x must satisfy 2x ≤ b. So we can restrict x ≤ floor(b/2). So x=3 has 2*3=6 > b=4, so infeasible. So we can loop x from 0 to min(a, floor(b/2)). That simplifies.
Thus for each x from 0 to min(a, floor(b/2)), compute y = min( floor(c/2), b - 2x ), total = x + y.
Find max total.
For example1: a=3, floor(b/2)=2, so x max 2. x=0: total=0+2=2; x=1: total=1+2=3; x=2: y = min(2, 4-4=0) =0 -> total=2. Max =3 => answer = 3*3=9. Matches.
Example2: a=1,b=0,c=5. floor(b/2)=0, so x can be 0 only. x=0: y = min(floor(5/2)=2, b-0=0) => y=0 => total=0 => answer 0. matches.
Example3: a=5,b=3,c=2. floor(b/2)=1 (since 3/2=1). So x up to min(5,1)=1.
x=0: y = min(floor(2/2)=1, b-0=3) => y=1 => total=1.
x=1: y = min(1, 3-2=1) => y=1 => total=2.
Max total=2 => answer=6. matches.
Thus solution.
But wait, we must also consider possibility of performing op2 before op1 could allow more operations if doing op1 first uses heap2 that could be used for op2? But our total constraints account for total usage of heap2. However, note that op2 consumes 1 from heap2, op1 consumes 2 from heap2. So the total usage of heap2 is 2x + y. So it's captured. But is there any scenario where doing op2 first, then op1, allows more total operations than any (x,y) satisfying constraints? Let's check. Suppose we try to do more operations by not fixing a particular order? But total stones consumed from each heap cannot exceed initial. So any sequence yields some x and y. So the maximum possible total operations is exactly max of x+y subject to constraints. So it's correct.
But wait, we might consider performing operations in a different way? The operations are only those two types. So yes.
Thus solution: For each test case, compute max_total_ops = 0; For x from 0 to min(a, b//2) (since 2x ≤ b), y = min(c//2, b - 2*x); total = x + y; update max. Answer = max_total_ops * 3.
But careful: The answer is the maximum number of stones taken, which is 3*(x+y). So compute that.
Alternatively, we can think of it as: We can also do symmetric approach: treat the problem as maximizing number of operations of two types. Since both types give 3 stones, answer is 3 times that. So fine.
But note: there is nuance: what if we perform operations in a different order that uses heap2 in a way that allows more than b-2x? Actually total consumption of heap2 is fixed: each op1 uses 2, each op2 uses 1. So the total number of heap2 stones used is 2x + y. Since we only have b, we have 2x + y ≤ b. So that's a constraint.
Thus algorithm stands.
But let's also think if there is any possibility to use stones from heap1 or heap3 in ways not captured? Only these operations.
Edge cases: all zero -> answer 0.
Now, maybe there is an even simpler approach: Since each operation gives 3 stones, and total stones in heaps are a+b+c, but we can't take all because of constraints. But perhaps we can compute maximum number of triples (1,2,0) and (0,1,2) from (a,b,c). It's like packing. This is similar to: given counts of "1-2" pairs across two adjacent positions.
We can also solve by considering the number of operations of type2 (y) limited by c//2. Then we can compute how many type1 operations we can do given leftover b after using y. Since each type1 uses 2 from b and 1 from a. So for a given y, x = min(a, floor((b - y)/2)). And we need b - y ≥ 0. So maximize y + min(a, floor((b - y)/2)) for y from 0 to c//2. That's symmetric. Both ways.
Given small constraints, either is fine.
We'll implement.
But also note that the problem asks for maximum stones taken, not number of operations. So output 3 * max_ops.
Double-check example: first case, answer 9, yes.
Now, let's test some random cases.
Case: a=100, b=100, c=100.
Compute max: x up to min(100, 100//2=50) => x up to 50. For each x, y = min(50, 100-2x). Let's find best. For x=0, y=50 => total=50. x=1, y=50? Actually y = min(50, 100-2=98) =>50, total=51. x=2, y=50, total=52. ... Up to x where 100-2x >=50? That is x <= 25. So for x=25, 100-50=50 => y=50, total=75. For x=26, 100-52=48, y = min(50,48)=48, total=74. So max at x=25 gives 75. But check if we can do better? Could there be x=50? Then y = min(50, 100-100=0) =0, total=50. So max total ops = 75. So stones = 225. Let's see if constraints: x=25 uses 25 from a, uses 225=50 from b, plus y=50 uses 1 each from b? Actually y=50 uses 1 each from b? Wait y=50 uses 1 each from b? Op2 consumes 1 from heap2 per operation, so total b used = 2x + y = 50 + 50 = 100. That's exactly b. So b fully used. a used 25 (out of 100). c used 2y=100 (out of 100). So total stones taken = 25+100+100? Actually stones taken: from a: 25, b: 100, c: 100 =225, which is 375. That's plausible. Could we do more? Suppose we try x=33, then 2x=66, b left 34, y <=34 and <=50, so y=34, total=67, which is less than 75. So indeed max 75. But is it possible to achieve 76? That would need x+y=76. Then 2x+y ≤ b = 100 => (2x + y) = x + (x+y) ≤ 100 => x + 76 ≤ 100 => x ≤ 24. So x ≤24. Then total 76 means y = 76 - x. Need y ≤ c/2 = 50 => 76 - x ≤ 50 => x ≥ 26. Contradiction. So 75 is max.
Seems correct.
Thus solution is straightforward.
Now implement.
But wait, we need to be careful: In the operation definitions, "take one stone from the first heap and two stones from the second heap" means after taking, the stones are removed from heaps. But Alice collects them. She wants maximum stones she can take. She can stop anytime. So we want max total stones collected, which is 3*(#ops). So fine.
Edge case: If we have leftover stones that cannot be used in any operation, they are not taken. So answer is as above.
Given that a,b,c up to 100, we could also brute force all possibilities with loops for x and y.
Implementation steps:
For each test case:
- Read a, b, c.
- ans = 0
- for x in range(0, min(a, b//2)+1):
y = min(c//2, b - 2*x)
total_ops = x + y
ans = max(ans, total_ops)
- print(ans*3)
Check: b - 2*x could be negative? But we restrict x up to b//2 so it's non-negative.
Alternatively, we can loop y from 0 to c//2 and compute x similarly.
Let's test with provided examples.
Case1: a=3,b=4,c=5:
b//2=2, min(3,2)=2 => x=0..2
x=0: y = min(2,4) =2 => total=2
x=1: y = min(2,4-2=2)=2 => total=3
x=2: y = min(2,4-4=0)=0 => total=2 => max=3 => ans=9
Case2: a=1,b=0,c=5:
b//2=0 => x in [0]? min(1,0)=0 => only x=0 => y = min(2,0)=0 => total=0 => ans=0
Case3: a=5,b=3,c=2:
b//2=1 => min(5,1)=1 => x=0..1
x=0: y = min(1,3)=1 => total=1
x=1: y = min(1,3-2=1)=1 => total=2 => ans=6
Matches.
Now we should consider if there is any scenario where using a different ordering than all op1 then op2 might allow more operations due to some "interleaving" effect? Our derived constraints are necessary and sufficient; we already argued that any (x,y) satisfying can be realized. However, is there any subtle condition about the order that could allow a combination not captured by a fixed x and y? For instance, could we perform some operations partially so that we temporarily use stones from one heap to enable more of the other type? But the total consumption is still bounded by initial counts. In typical resource allocation problems, the constraints we derived are necessary and sufficient because the operations are linear and the resources are non-replenishable. The only possible issue is if the preconditions require that at each operation, the required stones are present; but as we argued, the simple order (all type1 then type2) works if total constraints hold. But what about the scenario where doing type2 first might allow more type1 because type1 also uses heap2 and heap1, but doing type2 first reduces heap2 and heap3, but we already accounted for that. There's no benefit to interleaving in terms of total counts; the total counts of each heap used are additive. So the set of achievable (x,y) is exactly those satisfying the inequalities.
Thus solution is correct.
But we should double-check with some random simulation for small numbers to be thorough. Let's try mental test: a=2, b=3, c=2.
Our method: b//2=1 => x max=1.
x=0: y = min(1,3)=1 => total=1
x=1: y = min(1,3-2=1)=1 => total=2.
So max total ops=2 => answer=6.
Now can we achieve 2 operations? x=1,y=1. Check constraints: heap1 used 1 ≤2, heap2 used 21+1=3 ≤3, heap3 used 21=2 ≤2. So it's possible. Sequence: do op1 first: requires heap1>=1, heap2>=2. Initially heap1=2, heap2=3, ok. After op1: heap1=1, heap2=1, heap3=2. Then op2: requires heap2>=1, heap3>=2. heap2=1, heap3=2, ok. After op2: heap1=1, heap2=0, heap3=0. Total stones taken = 6. So works.
What about a=2,b=3,c=1? c//2=0 => y max=0. b//2=1 => x max=1. x=0: y=0 total=0; x=1: y=0 total=1 => ans=3. Could we do something like op2? No because c=1 can't do op2. Only op1 once possible. That's correct.
Now a=0,b=100,c=100: Then only op2 possible because op1 requires a>0. Our method: x max = min(0,100//2=50) = 0. So x=0 only. y = min(50,100) =50 => total=50 => ans=150. Check manually: can do up to 50 op2 because each uses 2 from c, so limited by c//2=50; heap2 consumption y=50 ≤100. So 50*3=150. Good.
a=100,b=100,c=0: only op1 possible: x max = min(100,50)=50. y=0 always. total=50 => ans=150. Check: each op1 uses 2 from b, so limited by b//2=50; also a≥50. So 50*3=150.
a=1,b=2,c=100: b//2=1 => x max= min(1,1)=1.
x=0: y = min(50,2)=2? Actually y = min(50, b=2) =>2 => total=2.
But careful: if x=0, y=2, total ops=2, stones=6. But check feasibility: y=2 op2 uses heap2: 2*1? Actually each op2 uses 1 from heap2, so total heap2 used = y = 2, which is ≤ b=2. Heap3 used 2y=4 ≤100. So total ops=2. However, is x=0, y=2 feasible? Perform two op2: first op2: need heap2>=1, heap3>=2. Initially heap2=2, ok. After first op2: heap2=1, heap3=98. Second op2: need heap2>=1, heap3>=2: heap2=1, ok. So yes. So total ops=2, answer=6. But our method with x=0 gave y=min(50,2)=2, total=2. x=1: y = min(50, b-2=0) =>0, total=1. So max=2. So answer 6. Good.
Now consider a=2,b=4,c=3: c//2=1. b//2=2 => x max = min(2,2)=2.
x=0: y = min(1,4)=1 => total=1
x=1: y = min(1,4-2=2)=1 => total=2
x=2: y = min(1,4-4=0)=0 => total=2
max total=2 => answer=6. Check if we can get 3? That would require x+y=3. Possibilities: (x=2,y=1) uses heap2=2*2+1=5 >4, impossible. (x=1,y=2) uses heap3=4>3, impossible. (x=0,y=3) uses heap3=6>3, impossible. So max 2.
But consider a=2,b=5,c=3: c//2=1. b//2=2 => x max=2.
x=0: y=min(1,5)=1 => total=1
x=1: y=min(1,5-2=3)=1 => total=2
x=2: y=min(1,5-4=1)=1 => total=3
So total ops = 3 => answer=9. Check feasibility: x=2, y=1. Heap1 used 2, heap2 used 22+1=5, heap3 used 21=2? Actually c=3, 2y=2, ok. So total consumption: a=2, b=5, c=2 (since 2 used). So stones taken =2+5+2=9. Sequence: do op1 twice? But after two op1, we need heap2 left: initial b=5, after two op1: each uses 2 from b, so after two: b=1? Actually 5-4=1, a becomes 0? Actually a=2-2=0. Then do op1? Wait x=2 means two op1. Then we want to do one op2. After two op1: a=0, b=1, c=3. Then op2 requires b>=1, c>=2: b=1, c=3, okay. So yes. So total operations=3, answer=9. Works.
Now consider a=5,b=3,c=5: b//2=1 => x max=1.
x=0: y=min(2,3)=2? floor(c/2)=2, b=3 => y=2 total=2.
x=1: y=min(2,3-2=1)=1 total=2.
max=2 => ans=6. But maybe we could do op2 twice and then op1? If we do op2 twice first: after two op2: b reduces by 2 (each uses 1) => b=1, c reduces by 4 => c=1. Then we can't do op1 because need b>=2. So only two operations. If we do op1 first: after op1: a=4, b=1, c=5. Then we can do at most one op2? Actually after op1, b=1, so can do one op2 (needs b>=1, c>=2) => c becomes 3. That's total 2 operations. So indeed max 2.
Now consider a=5,b=10,c=5: b//2=5, x max = min(5,5)=5.
Compute for each x:
x=0: y = min(2,10)=2 total=2
x=1: y = min(2,10-2=8)=2 total=3
x=2: y = min(2,10-4=6)=2 total=4
x=3: y = min(2,10-6=4)=2 total=5
x=4: y = min(2,10-8=2)=2 total=6
x=5: y = min(2,10-10=0)=0 total=5
max total=6 => answer=18. Check feasibility: x=4,y=2. Then heap1 used 4, heap2 used 2*4+2=10, heap3 used 4. So all resources fully used? a=5 remains 1 unused, c=5 remains 1 unused, b exhausted. Total stones taken=4+10+4=18. Sequence: do 4 op1, then 2 op2? After 4 op1: a becomes 1, b becomes 10-8=2, c=5. Then do 2 op2? Need b>=1 each, c>=2 each. After first op2: b=1, c=3. After second op2: b=0, c=1. So works.
Now consider a=5,b=5,c=5: b//2=2, x max=2.
x=0: y=min(2,5)=2 total=2
x=1: y=min(2,5-2=3)=2 total=3
x=2: y=min(2,5-4=1)=1 total=3
max=3 => answer=9. Could we do 4? Possibly x=1,y=3? But y limited by c//2=2, so no. So max 3.
Alright.
Thus solution seems correct.
Now we should implement in code.
We need to read t, loop.
Potential pitfalls: Input reading, handling integers.
Edge case: b may be odd, floor division okay.
Thus final solution.
But I've seen similar problems on Codeforces (like "Collecting Coins" or "Three Piles"? Actually this is from Codeforces Round #... But anyway).
I'll produce Python code.
Time complexity O(t * min(a, b//2)). Since a,b up to 100, fine.
Alternative solution: Use binary search or greedy? But not needed.
Now confirm that there is no trick: The operations both take exactly 3 stones, so maximizing number of operations is correct. But what if there is a possibility of not using all stones from a heap because we might want to reserve for later? That's already considered in constraints; we choose x and y to maximize total.
Thus answer.
Wait, could there be a scenario where doing an operation of type1 reduces ability to do type2, but by choosing a different order we could do more total operations? That would mean that for some feasible (x,y) satisfying totals, we might not be able to realize because order matters. But we gave a constructive order: all type1 first then type2. That requires after type1, we have enough heap2 for type2. This is exactly y ≤ b - 2x. That's already in constraints. So any (x,y) satisfying y ≤ b-2x is realizable by that order. So yes.
But what about doing type2 first then type1? That would require x ≤ a and also after type2, we have enough heap2 for type1: need 2x ≤ b - y. Which is same condition 2x + y ≤ b. So either order works. So if constraints hold, we can do either.
Thus solution is correct.
Now I'll write solution explanation.
However, note that the problem statement says "Alice is playing with some stones. Now there are three numbered heaps of stones. The first of them contains a stones, the second of them contains b stones and the third of them contains c stones. Each time she can do one of two operations... She wants to get the maximum number of stones... Can you help her?" The output is the maximum possible number of stones that Alice can take after some operations.
Thus answer as derived.
But check if there is any possibility of using stones from heap1 and heap3 together directly? No, only the two operations.
Thus final solution.
I'll now produce a Python solution:
def solve():
import sys
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
a = int(next(it)); b = int(next(it)); c = int(next(it))
max_ops = 0
max_x = min(a, b//2)
for x in range(max_x + 1):
y = min(c//2, b - 2*x)
max_ops = max(max_ops, x + y)
out.append(str(max_ops * 3))
sys.stdout.write("\n".join(out))
if name == "main":
solve()
That should work.
One more check: Suppose a=0, b=1, c=2. b//2=0 => x max = 0. x=0 => y = min(1, b-0=1)?? Actually c//2 = 1, b=1 => y = min(1,1) = 1 => total = 1 => answer = 3. But is that possible? We have heap2=1, heap3=2. Op2 requires heap2>=1 and heap3>=2, so we can do one op2. Yes. So answer 3.
Now a=0, b=1, c=1. c//2=0 => y=0 => total=0 => answer=0.
Now a=0, b=2, c=1. b//2=1 => max_x = min(0,1)=0 => x=0 => y = min(0, 2)=0 => total=0 => answer=0. But could we do op1? No because a=0. Could we do op2? Need c>=2, so no. So answer 0.
Now a=1,b=1,c=2. b//2=0 => x max = min(1,0)=0 => x=0 => y = min(1, 1)=1 => total=1 => answer=3. Check: op2 possible? heap2=1, heap3=2, yes, one op2. So answer 3. But can we do op1? No, heap2<2. So indeed max=3.
But note: a=1,b=1,c=2, we also could consider doing op2 first, then maybe op1? After op2, heap2 becomes 0, heap3 becomes 0, heap1 stays 1. No further operations. So total 3.
Now a=2,b=1,c=2: b//2=0 => x=0, y= min(1,1)=1 => total=1 => answer=3. Can't do op1 because b<2. So yes.
Now a=2,b=2,c=2: b//2=1 => x max= min(2,1)=1.
x=0: y = min(1,2)=1 => total=1
x=1: y = min(1,2-2=0)=0 => total=1
max=1 => answer=3. But can we do more? Let's see if we can do both op1 and op2? If we try op1 first: need b>=2, a>=1. After op1: a=1, b=0, c=2. Then cannot do op2 because b=0. If we try op2 first: need b>=1, c>=2. After op2: a=2, b=1, c=0. Then cannot do op1 because b=1 <2. So only one operation, total 3. Good.
Now a=2,b=3,c=2: b//2=1 => x max=1.
x=0: y=min(1,3)=1 => total=1
x=1: y=min(1,3-2=1)=1 => total=2 => answer=6. Check: do op1 then op2? After op1: a=1, b=1, c=2. Then op2: b>=1, c>=2 ok. So two ops -> 6. Works.
Thus solution is robust.
Thus final answer.
We should also consider if there is any scenario where using the operations in a different pattern (like doing op2, then op1, then op2) could allow more operations than the simple x and y counts. But the counts x and y are total numbers; any sequence yields integer counts for each operation. So the maximum is max over integer (x,y) satisfying constraints. So it's fine.
Thus final.
Now, I'll produce a clear explanation.
But note: The problem is Codeforces Round #... I recall a problem "Collecting Coins" maybe not. But anyway.
One more thought: Could there be an optimal solution that uses fractional operations? No.
Thus final solution as above.
Let's produce final answer.
```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
a = int(next(it))
b = int(next(it))
c = int(next(it))
max_ops = 0
# x = number of operations of the first type
max_x = min(a, b // 2) # need 2x ≤ b
for x in range(max_x + 1):
y = min(c // 2, b - 2 * x) # y = number of second type operations
max_ops = max(max_ops, x + y)
out_lines.append(str(max_ops * 3))
sys.stdout.write("\n".join(out_lines))
if name == "main":
solve()