We have a problem: Polycarp has a sticks and b diamonds. He can craft either:
- Shovel: uses 2 sticks, 1 diamond → yields 1 emerald.
- Sword: uses 2 diamonds, 1 stick → yields 1 emerald.
Goal: maximize number of emeralds produced.
This is a typical resource allocation problem: we have two types of items, each uses two of one resource and one of the other. Equivalent to: given a and b, we can make at most something like min( (a+b)/3, min(a,b) )? Actually let's derive.
We want to maximize the number of items (emeralds) we produce. Each item consumes total of 3 resources: either (2,1) or (1,2). So total resources consumed is 3 per item. However, constraints: we cannot exceed a sticks and b diamonds.
Let x = number of shovels (using 2 sticks, 1 diamond)
Let y = number of swords (using 1 stick, 2 diamonds)
Constraints:
2x + y ≤ a
x + 2y ≤ b
We want to maximize x + y.
This is a linear programming problem in integers, but since constraints are linear, the max will be integer (maybe with some rounding). The feasible region and objective is linear, the max will be achieved at an extreme point. However, because the coefficients are integers and the RHS integers, the integer optimum may be less than the LP relaxation's optimum, but given the specific structure, we can find a closed form.
Simplify: Add constraints:
Total sticks: 2x + y ≤ a
Total diamonds: x + 2y ≤ b
We can combine: add both: 3(x+y) ≤ a+b → x+y ≤ floor((a+b)/3)
But also each item uses at least one of each resource? Not necessarily: It uses either 2 sticks 1 diamond OR 1 stick 2 diamonds. So it always uses at least one stick and at least one diamond? Actually a shovel uses 1 diamond, so if you only make shovels, each uses 1 diamond; swords use 1 stick. So any item uses at least one of each? Check: Shovel uses 2 sticks, 1 diamond. So it uses 1 diamond. Sword uses 1 stick, 2 diamonds. So each item consumes at least one stick and at least one diamond? Actually shovel uses 2 sticks and 1 diamond, so it uses at least one diamond but also uses sticks; it always uses both types? It uses both: shovel uses both stick and diamond, sword also uses both. So each product uses at least one of each resource. That implies that we cannot produce more than min(a, b) items? Not exactly: Because one item always consumes at least one stick and at least one diamond, but can consume up to 2 of one of them. So if one resource is scarce, it's limiting. Let's check: Suppose a=100, b=1. Then you can only make at most 1 item? Because each item requires at least 1 diamond. But could we make more than 1? The one diamond can be used for a shovel (since shovel uses 1 diamond) but that shovel also uses 2 sticks. But after using that one diamond, no more diamonds. So you can only make at most 1 item. But min(a,b)=1, indeed that is a bound. But also we might have a scenario where min(a,b) is large but the sum bound is smaller? Actually if a=2, b=2: min=2, sum=4, floor(4/3)=1, so the sum bound is tighter: you can't make 2 items because each requires 3 resources total. Actually can we make 2 items? With a=2, b=2, possibilities: make one shovel (2 sticks, 1 diamond) uses 2,1 leaves 0,1. Then cannot make another because need at least 1 stick and 2 diamonds? Actually left 0 sticks, 1 diamond. So cannot make another. Could we make one sword (1 stick,2 diamonds) uses 1,2 leaves 1,0, cannot make another. Could we make something else? There is no other. So max=1. So min(2,2)=2 is not an upper bound; the actual bound is floor((a+b)/3)=1. So the maximum is min( floor((a+b)/3), a, b )? But for a=2, b=2, floor((2+2)/3)=1, min(1,2,2)=1 works. For a=4, b=4: sum=8 floor(8/3)=2, min(2,4,4)=2, matches sample. For a=1000000000, b=0: floor( (1e9)/3 ) approx 333333333, but min(333333333,0,1e9) = 0, because b=0, so output 0. Works. For a=7,b=15: sum=22 floor(22/3)=7, min(7,7,15)=7, matches sample. For a=8,b=7: sum=15 floor(5) =5, min(5,8,7)=5 matches sample.
But is it always exactly min( floor((a+b)/3), a, b )? Not exactly: Consider a=1, b=1: floor(2/3)=0, min(0,1,1)=0, correct (cannot make any item because need at least 2 of one resource? Actually each item requires either 2 sticks or 2 diamonds? Wait, if a=1,b=1, we cannot craft a shovel (needs 2 sticks) nor a sword (needs 2 diamonds). So 0 is correct). a=2,b=1: floor(3/3)=1, min(1,2,1)=1, but can we craft 1 item? Yes, we can craft a shovel (2 sticks, 1 diamond) uses exactly resources. So output 1. a=1,b=2: floor(3/3)=1, min(1,1,2)=1, craft a sword. Good. a=3,b=0: floor(3/3)=1, min(1,3,0)=0, actual: cannot craft because no diamonds, output 0. So that works. a=3,b=3: floor(6/3)=2, min(2,3,3)=2, can we craft 2 items? Options: one shovel and one sword: total sticks = 2+1=3, diamonds = 1+2=3, yes. So 2. a=5,b=0? min(0) =0. So seems consistent.
But is there any case where min(a,b) is less than floor((a+b)/3) but still the answer is less than min(a,b)? Suppose a=2,b=100: min=2, floor(102/3)=34, min=2, but can we craft 2 items? Check: to craft 2 items, we need resources. Let's see if 2 shovels: 4 sticks, 2 diamonds => not enough sticks. 2 swords: 2 sticks, 4 diamonds => sticks=2, diamonds=100, that works (uses 2 sticks, 4 diamonds) -> yes, you can craft 2 swords. So answer=2 = min(a,b). Actually min(a,b)=2. But what about a=3,b=100? min=3, floor=34, min=3. Can we craft 3 items? Let's try 3 swords: sticks=3, diamonds=6 -> works, so 3. So answer=3. So seems answer = min( a, b, floor((a+b)/3) ) works.
But is there scenario where answer is less than min(a,b)? Possibly when one of them is not enough to cover the "two of one" requirement. But min(a,b) only gives the minimum of the two counts, but you need that each item consumes at least one of each resource. So if min(a,b) is say 5, you need at least 5 of each, but you also need that the other resource can supply the extra needed for the items that use 2 of that resource. But since each item uses exactly 1 of the scarce resource (the min one) if we choose the appropriate distribution, we can allocate the scarce resource as the one used only once per item. For example, if sticks are scarce (a <= b), then we can focus on making swords (which use 1 stick per item) to maximize usage of sticks, because each sword uses 1 stick and 2 diamonds. With a sticks, we can at most make a swords, provided we have enough diamonds: need 2a diamonds. If b >= 2a, then we can make a items. If b < 2a, then we cannot make a swords because diamonds insufficient. But we could also make some shovels to use more diamonds? Actually if sticks are scarce, we want to use each stick as efficiently as possible: each sword uses 1 stick and yields 1 emerald; each shovel uses 2 sticks and yields 1 emerald, which is less efficient in terms of sticks. So to maximize total items, we should use items that use only 1 stick per emerald. So the maximum number of items when a <= b is at most a (since each item consumes at least 1 stick). But if b < 2a, we might not have enough diamonds to pair with each stick as a sword (since each sword uses 2 diamonds). But we could mix shovels to use diamonds more efficiently? Actually a shovel uses 1 diamond per emerald, while a sword uses 2 diamonds per emerald. So if diamonds are the limiting factor for swords, we might want to use shovels which use fewer diamonds. But shovels use more sticks, which are already scarce. So there's a tradeoff.
Let's formalize: We want to maximize x+y subject to constraints. This is integer linear programming with two variables. The optimal solution for LP relaxation can be found by solving:
Let total items = t = x+y. Express x = t - y. Constraints become:
2(t-y) + y = 2t - y ≤ a => y ≥ 2t - a.
x + 2y = (t - y) + 2y = t + y ≤ b => y ≤ b - t.
Also y ≥ 0 and x ≥ 0 => y ≤ t, y ≥ 0.
Feasibility requires existence of integer y satisfying max(0, 2t - a) ≤ y ≤ min(t, b - t). So feasible t if max(0, 2t - a) ≤ min(t, b - t).
Given a and b, we want maximum integer t such that condition holds.
Alternatively, consider the LP optimum: Solve without integer constraints. The optimal value is min( a, b, floor((a+b)/3) )? Not exactly; LP relaxation gives t ≤ (a+b)/3 and also t ≤ a (from stick constraint: 2x+y ≤ a, but if y negative? Wait from stick constraint: 2x+y ≤ a. Since x,y ≥ 0, then 2x+y ≥ x+y = t? Actually, because x ≥ 0, 2x+y ≥ x+y = t, so t ≤ a. Similarly from diamond constraint: x+2y ≥ x+y = t, so t ≤ b. So t ≤ min(a,b) is indeed implied (since t = x+y ≤ 2x+y ≤ a? Not necessarily: t = x+y, we know 2x+y ≤ a, but because x ≥ 0, 2x+y ≥ x+y = t? Yes, since 2x+y = x + (x+y) ≥ x+y = t because x≥0. So t ≤ 2x+y ≤ a => t ≤ a. Similarly t ≤ b. So these are necessary conditions: t ≤ a, t ≤ b, and t ≤ (a+b)/3. So the LP optimum is t* = min(a, b, (a+b)/3). Since the constraints are linear, the optimum is at a vertex where at least one constraint is tight. However, the integrality may cause it to be floor of that if it's not integer.
But is the LP optimum always achievable? Since the constraints are linear and the objective is integral, the maximum integer t should be floor of the LP optimum? Not always; consider a situation where min(a,b, (a+b)/3) = some fractional value, say a=2,b=2: min(2,2,4/3)=4/3. floor=1, which is feasible. Could there be a case where floor(min(...)) is not feasible? Let's test borderline: Suppose a=1,b=2: min(1,2,1)=1, floor=1 feasible. Suppose a=0,b=2: min(0,2,2/3)=0 feasible. Suppose a=5,b=1: min(5,1,2)=1? Actually (5+1)/3=2, min(5,1,2)=1. floor=1, feasible? Check: a=5,b=1: can make 1 shovel (2 sticks,1 diamond) uses 2,1 leaves 3,0 -> yes 1 item. So feasible.
What about a=4,b=1: min(4,1,5/3≈1.666)=1, feasible? 1 shovel. Yes. a=3,b=1: min(3,1,4/3≈1.333)=1, feasible: 1 shovel uses 2 sticks,1 diamond, leaves 1,0. So yes.
But consider a=2,b=2 we already saw feasible. What about a=2,b=3: min(2,3,5/3≈1.666)=1.666, floor=1, feasible? 1 shovel uses 2,1 leaves 0,2 -> can't do second? Could we make 2 items? Let's check: can we make 2 items? To make 2 items, constraints: 2x+y ≤ 2, x+2y ≤ 3, x+y=2, x,y≥0 integer. Solve: x+y=2 => y=2-x. First constraint: 2x+(2-x)=x+2 ≤ 2 => x ≤ 0 => x=0. Then y=2. Check second: 0+4=4 ≤ 3? No. So cannot. So max=1. So floor(min)=1 works.
What about a=3,b=3 we saw 2 works, min(3,3,2)=2. Good.
What about a=5,b=5: min(5,5,10/3≈3.333)=3.333, floor=3. Can we make 3? Possibly yes: one solution: x=1,y=2: sticks:21+2=4 ≤5, diamonds:1+4=5 ≤5, sum=3. Or x=2,y=1: 22+1=5,2+2=4 ≤5, sum=3. So 3 works. Could we do 4? Check: sum constraint: 4*3=12>10, no. So max=3. Good.
What about a=6,b=3: min(6,3,9/3=3)=3, floor=3. Can we make 3? Try x? Since b is smaller: we might use swords? Actually b=3, a=6. If we use swords: each sword uses 2 diamonds and 1 stick. Max swords = floor(b/2)=1 if we only use swords? Actually swords use 2 diamonds each, so max swords possible = floor(3/2)=1, using 1 stick and 2 diamonds, leaving sticks=5, diamonds=1. Then we could maybe make a shovel: 2 sticks, 1 diamond, uses 2,1 leaves sticks=3,diamonds=0, total 2 items. So that's 2. But we can do better: maybe mix: Let x= shovels, y= swords. Want x+y=3. Then constraints: 2x+y ≤ 6, x+2y ≤ 3. Try y=0: 2x≤6 -> x≤3, x+0≤3 -> x≤3, so x=3 works: 3 shovels uses 6 sticks, 3 diamonds? Actually 3 shovels use 31=3 diamonds, and 32=6 sticks, both exactly. So x=3,y=0 works! So answer=3. So min constraint was 3, works.
Thus the formula min(a,b,floor((a+b)/3)) appears valid. But we should be cautious: min(a,b,floor((a+b)/3)) is equivalent to min( floor((a+b)/3), a, b ). But since a and b are integers, the minimum is an integer, obviously. But we need to ensure that this integer is always achievable. The typical solution for this problem (I recall it's from Codeforces Round something) is that answer = min( (a+b)/3, a, b ), but careful with integer division. Actually the known problem: "Polycarp and Sums of Subsequences"? No. This is similar to "Shovels and Swords" (or "Magic Stones"? Actually I recall a Codeforces problem: "A. Juggling Letters"? No. There is "A. FashionabLee"? Not that. There's a problem "A. Shovels and Swords" maybe. I've seen it: given a and b, the answer is min((a+b)//3, a, b). Yes.
But let's double-check some edge cases: a=10^9, b=10^9, then sum=2e9, floor(2e9/3)=666,666,666.666? Actually 2e9/3 = 666,666,666.666..., floor = 666,666,666. min(666,666,666, 10^9, 10^9)=666,666,666. That should be feasible? We can produce 666,666,666 items? Let's test feasibility: Need total sticks: 2x+y =? We need to find x,y such that x+y=666,666,666 and constraints. Solve via linear programming: The condition for existence of nonnegative x,y is that the two constraints combine to: 2x+y ≤ a, x+2y ≤ b, with sum fixed. The necessary and sufficient condition for existence of nonnegative (x,y) given sum t is: we need to be able to allocate such that the constraints hold. There's a known condition: t ≤ a, t ≤ b, and t ≤ (a+b)/3. That's necessary but is it sufficient? Let's consider we need to show that if t ≤ min(a,b,(a+b)/3), there exists integer x,y ≥0 with x+y = t. For given t, we need to find y satisfying max(0, 2t - a) ≤ y ≤ min(t, b - t). So existence condition is max(0, 2t - a) ≤ min(t, b - t). So we must check that for t = floor(min(...)) this holds. We can reason: Since t ≤ a and t ≤ b, we have 2t - a ≤ t (because t ≤ a => 2t - a ≤ t), and b - t ≥ 0. Also need that 2t - a ≤ b - t (since max lower bound could be 2t-a if it is non-negative, or 0 otherwise, and upper bound is min(t, b-t)). So we need to ensure 2t - a ≤ b - t (if 2t - a > 0). This is equivalent to 3t ≤ a + b, which is t ≤ (a+b)/3. So if t satisfies t ≤ a, t ≤ b, and 3t ≤ a+b, then the lower bound max(0,2t-a) is at most the upper bound b-t (since 2t-a ≤ b-t). Also we need that lower bound ≤ t (obviously 2t-a ≤ t because t ≤ a, also 0 ≤ t). And upper bound b-t might be less than t? But we take min(t,b-t). So we need lower bound ≤ min(t,b-t). This requires lower bound ≤ b-t (already holds from 3t ≤ a+b) and also lower bound ≤ t (holds as argued). So condition holds. Therefore there exists some y. Also the y can be chosen integer because bounds are integer (since a,b,t integers). So existence is guaranteed. So the maximum t is the largest integer satisfying t ≤ a, t ≤ b, 3t ≤ a+b. That's exactly min( a, b, floor((a+b)/3) ). So answer is min( a, b, (a+b)//3 ).
Thus the solution is straightforward: For each test case, compute ans = min(a, b, (a+b)//3).
Now note that a,b up to 10^9, t up to 1000, so trivial.
Thus output ans.
But sample input format: They have a line "Input" and then numbers, but in the problem statement they show "Input" and then a blank line, then "4", etc. So just parse t and then each a,b.
Thus the solution code is simple.
But wait, the example shows:
Input
4
4 4
1000000000 0
7 15
8 7
Output
2
0
7
5
Yes matches.
So I will craft the final answer: just compute min(a, b, (a+b)//3) for each test case.
Implementation in Python:
t = int(input().strip())
for _ in range(t):
a,b = map(int, input().split())
ans = min(a, b, (a+b)//3)
print(ans)
But be careful: The integer division // in Python yields floor.
All good.
Now we could also discuss alternative solution: while a>0 and b>0 and a+b>=3: but that would be slow for large numbers.
Thus final answer: as above.
But the user asked: "Polycarp plays a well-known computer game... How many emeralds can Polycarp earn... Input ... Output ..."
So we should produce the solution.
Thus we are done. ```python
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):
a = int(data[idx]); b = int(data[idx+1])
idx += 2
# Maximum number of items is the largest integer t such that:
# t ≤ a, t ≤ b, and 3t ≤ a + b
ans = min(a, b, (a + b) // 3)
out.append(str(ans))
sys.stdout.write("\n".join(out))
if name == "main":
solve()