Given 2 numbers n1 and n2, following operations can be performed:
Decrement n1 by 1(if n1>1)
Decrement n2 by 1(if n2>1)
Incremenet n1 by 1
Incremenet n2 by 1
Find the maximum possible value of gcd(n1,n2) with atmost k operations.
Note: Any of the 4 operations counts for one operation.
gcd(n1,n2) refers to GCD of n1 and n2
Input format:
First line of input contains single integer t, denoting no of test cases.
Each test case contains 3 lines with inputs n1 , n2 and k respectively on each line.
Output format:
For each test case print a single line containing the maximum possible value of gcd(n1,n2)
Constraints:
1 ≤ t ≤ 5
1 ≤ n1,n2 ≤ 10^5
0 ≤ k ≤ 10^2
SAMPLE INPUT
1
9
10
1
SAMPLE OUTPUT
10
Explanation
On incrementing 9 by 1, we get maximum possible GCD as 10 here.
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 need to maximize gcd(n1,n2) after at most k operations, where each operation can be: decrement n1 (if >1), decrement n2 (if >1), increment n1, increment n2. So we can change both numbers. With at most k moves, we can adjust each number by some integer (positive or negative) but there are restrictions: decrement only if >1, but we can also increment arbitrarily? The problem statement: "Decrement n1 by 1(if n1>1)" so you cannot decrement n1 below 1 (since if n1=1, can't decrement because would become 0, but maybe gcd with 0? But operations restricted). Also "Incremenet n1 by 1" no condition. So we can increase n1 arbitrarily, but decreasing is limited to a floor of 1. Similarly for n2.
Goal: maximize gcd(a,b) where a = n1 + x, b = n2 + y, with x,y integers, such that total operations = |x|? Actually each increment or decrement counts as one operation. If we increase n1 by d (d≥0) that's d operations. If we decrease n1 by d (d≥0) that's d operations but only allowed if n1-d >=1 (since each decrement step requires n1>1 at that step? The condition says "if n1>1" for each decrement operation. So we can only perform a decrement if before that operation n1>1. That means you cannot go to 0; you can only decrement down to 1. So effectively the final n1 must be at least 1. So final values a, b must be >=1? Could they be 0? Not if we follow decrement restrictions; also increment can't go below 1. So a,b >=1.
Also note that we can mix operations on both numbers. Total number of operations sum of absolute changes? Actually if we increase by d, that's d operations; if we decrease by d, that's d operations. So total operations = |delta1| + |delta2|, provided all intermediate steps are valid (i.e., when decreasing, we never go below 1). However, careful: If we need to decrease from n1 to a where a >=1, we need to perform decrement steps; each decrement requires current value >1. So if a is 1, it's fine because the last decrement goes from 2 to 1; but if we try to go to 0, not allowed. So as long as final a >=1, we can decrement to that value provided the path is valid: we start from n1, and each step we decrement only if current >1. If n1>1, we can decrement until we reach 1; if n1=1, we cannot decrement at all. So the reachable values by decrement are all integers from n1 down to 1 (provided we only decrement, but we could also increase then decrease? But that would be wasteful). But we can also combine increments and decrements. However to get a particular a, we can adjust using any sequence; the minimal number of operations to go from n1 to a is: if a >= n1, then need a - n1 increments. If a < n1, then we need n1 - a decrements, but only possible if we can decrement each step, which requires that during the process, we never attempt to decrement when value=1? But if n1 > a, and a>=1, then we can just decrement step by step. However, is it allowed to decrement from 2 to 1? Yes, because at that step, current value is 2 which is >1, so allowed. After that, if a=1, we stop; no further decrement needed. So we can reach any a in [1, n1-1] via n1-a decrements. But what about a value less than 1? not allowed. So reachable a's are any integer >=1? Actually we can also increase beyond n1 arbitrarily, so any integer a >=1 is reachable? But if a is less than 1 (i.e., 0 or negative), not allowed. So a ∈ ℕ (positive integer). However, if we want a < n1, we need to decrement; if n1=1, we cannot decrement, so a cannot be less than 1; but a=1 is current, so a can be 1. So indeed reachable a values are: if n1=1, then a can be any integer >=1? But wait: from n1=1, we can only increment (since decrement not allowed). So we can reach any a >=1 via increments. So yes, a can be any integer >=1. Similarly for b.
But there is subtlety: Could we achieve a value lower than n1 by first incrementing then decrementing? That would be wasteful but might allow something like: from n1=1, we cannot decrement, so we can't reach a=0 (not allowed anyway). But could we reach a=2? Yes increment. So indeed any integer a >=1 is reachable from any starting n1? For n1=2, to get a=3, we increment. To get a=1, we decrement once. So yes, any a >=1 is reachable with minimal operations = |a - n1|, as long as when a < n1, we are just decrementing. However, there is a nuance: The decrement operation condition is "if n1>1". Suppose n1=2, a=0: not allowed. But if we try to get a=0, we would need to decrement from 1 which is not allowed. So not reachable. So for a >=1, it's fine: the path from n1 to a via only decrements if a<n1: we go from n1 down to a. All intermediate values are > a >=1? Actually if a >=1 and a < n1, then during decrement steps, the values go from n1, n1-1, ..., a+1, a. The only step that could be problematic is the last decrement if a=0, but a>=1 so safe. Also when the current value is a+1, it's >1? Possibly if a=0 then a+1=1 but we don't go there. But if a=1, then the step from 2 to 1 is allowed because 2>1. So it's fine. So minimal operations = |a - n1|.
Similarly, if we want to use both numbers, total operations = |a - n1| + |b - n2|, as long as each step sequence is valid. Could there be a scenario where we need extra operations because we need to intermix to satisfy constraints? Not necessary; we can independently change each variable.
Thus the problem reduces to: Given positive integers n1, n2, and integer k >= 0, find the maximum possible d = gcd(a,b) over all positive integers a,b >=1 such that |a - n1| + |b - n2| <= k.
We want to maximize d.
We can choose a and b arbitrarily large (by incrementing) as long as total ops <= k. Also we can decrement them (to lower values) but not below 1.
We need maximum gcd.
Observations: gcd(a,b) <= min(a,b). But we can increase numbers so min can be large.
Potentially, we can aim for some large d, and try to see if we can make both a and b multiples of d with limited operations.
So the problem is: maximize d such that there exist multiples a = d * x, b = d * y, with x,y positive integers, and |dx - n1| + |dy - n2| <= k.
Additionally, a,b >=1, so d>=1.
Since k is up to 100, and n1,n2 up to 1e5, d could be large.
Brute-force over d? d can be up to n1 + k maybe? Actually we can increase n1 up to n1+k, so a_max = n1 + k (if all ops used on n1). Similarly b_max = n2 + k. So max possible a,b is roughly 1e5+100, so d <= min(max_a, max_b) ≤ ~1e5+100. But d could be as large as max a or b? But we need both multiples, so d cannot exceed min(a,b). So d max <= min(n1+k, n2+k) but that's up to ~1e5+100. So iterating d from high to low up to 1e5 maybe feasible? t up to 5, so 5 * 1e5 = 5e5, plus for each d need to check if we can achieve it within k ops. But we need to compute minimal cost to make both numbers multiples of d. The cost for a given d is: We need to choose some multiple a = d * ceil(n1/d) or floor? Actually we can adjust n1 to nearest multiple of d, either by increasing to the next multiple or decreasing to the previous multiple. But decreasing is only possible if that multiple >=1. Since d is positive, the previous multiple (floor) could be d * floor(n1/d). That's at least d, which is >=1. But if n1/d floor is 0? That would mean n1 < d, then floor(n1/d)=0, which gives a=0, not allowed because a must be >=1. So for n1 < d, the only multiples are d1 = d, d2, etc. The nearest multiple that is >=1 is d itself. So we need to consider multiples that are >=1. So effectively, for each d, we need to find a positive integer multiple md (m>=1) that minimizes |md - n1|, subject to that m*d >=1. Similarly for n2.
But wait: Could it be beneficial to adjust to a multiple that is not the nearest? Because sometimes we might coordinate between a and b to share the same d but perhaps choose multiples such that total ops is minimized? But the cost for each is independent, we can choose different multiples for each. The total cost is sum of distances to the chosen multiples. So for each d, the minimal total cost is: min_{a multiple of d, a>=1} |a - n1| + min_{b multiple of d, b>=1} |b - n2|. Because we can independently pick the best multiple for each. Indeed, we can choose a and b independently to minimize the sum. So it's just the sum of distances to the nearest multiple of d (with the constraint that multiple must be >=1). However, careful: The nearest multiple might be 0 if n1 < d and floor gives 0. But 0 is not allowed. So we need to consider the distance to the next multiple (d) only. So the minimal distance for n1 to a multiple of d is: if n1 >= d, then min(n1 % d, d - n1 % d) (i.e., distance to floor multiple or ceiling multiple, both >= d? Actually floor multiple = d * floor(n1/d) which is >= d >=1, so okay. Ceil multiple = d * ceil(n1/d) which is >= n1, also >= d). If n1 < d, then floor is 0 (invalid), so only ceil = d, distance = d - n1. So minimal cost for n1 =
if n1 >= d: min( n1 mod d, d - (n1 mod d) )
else: d - n1
But is it always optimal to choose the nearest multiple? Yes, because for any other multiple further away, distance is larger. So we can define cost1(d) = minimal number of operations needed to change n1 to a positive multiple of d.
Similarly cost2(d).
Then we need to find max d such that cost1(d) + cost2(d) <= k.
However, we must also consider that we could change numbers to non-multiples of d but still have gcd = d? For example, if a = d * p, b = d * q, then gcd is at least d, but could be greater if p and q share additional factor. But we aim for exactly gcd = d. However, we just want to maximize gcd(a,b). So if we find d such that we can achieve a,b with gcd exactly d or a multiple of d, but we only need the gcd to be >= d. Actually if we can achieve a,b such that gcd(a,b) >= d, that means there exists some d' >= d that divides both. So maximizing the gcd is equivalent to finding the largest d for which there exist a,b with total cost <= k and d divides both a and b. Because if there exists a pair with gcd = G, then for all d <= G, there exists a pair with both divisible by d? Wait: If we have a,b with gcd=G, then both are divisible by d for any d that divides G, but not necessarily for arbitrary d <= G. However, if we want to test a candidate d, we need to see if there exist a,b with total cost <= k such that d divides both a and b. But if the actual maximum possible gcd is G, then for d=G, it works. For d > G, it doesn't. So we can binary search or iterate d descending and check if condition holds. So checking condition "exists a,b both multiples of d with total cost <= k" is necessary and sufficient for d to be achievable as a divisor of the gcd? Actually careful: If we have a,b such that both are multiples of d, then gcd(a,b) is at least d, but could be larger. So if we can achieve such a,b, then the maximum possible gcd is at least d. So if we can find the largest d such that such a pair exists, that d is a lower bound for the maximum gcd. But could the maximum gcd be larger than any d for which we can make both numbers multiples of d? Suppose the maximum gcd achieved is G. Then for d=G, we can certainly find a,b both multiples of d (the ones that achieve G). So d=G satisfies condition. So the set of d for which condition holds includes all divisors of the gcd achieved, but also includes numbers that are less than or equal to that? Actually it includes all d that are less than or equal to the max gcd? Not necessarily: if we can achieve a,b with gcd=G, then for any d that divides G, we also have that a,b are multiples of d, so condition holds for d dividing G. But for d that do not divide G, condition might not hold even if d < G. Example: G=6, d=4: a,b are multiples of 6, but are they multiples of 4? Not necessarily. So d=4 may not be achievable even though 4 < 6. So the condition "exists a,b both multiples of d" is not monotonic in d: Larger d may be achievable even if some smaller d are not. However, for our purpose of finding the maximum possible gcd value, we can simply test candidate d from high to low; if we find a d that satisfies condition, then the answer is at least d, and we need to ensure that there isn't a larger d that also satisfies condition. Since we are looking for max d, we can search descending.
But the condition is that there exists some a,b multiples of d with total cost <= k. As argued, the minimal cost to make both multiples of d is cost1(d)+cost2(d). However, is it always optimal to pick the nearest multiple for each independently? Yes, because we can independently choose the multiple for each to minimize the sum. So if cost1(d)+cost2(d) <= k, then we can achieve at least d (by making n1 to that nearest multiple, n2 to its nearest multiple). That yields a,b both multiples of d, though their gcd might be a multiple of d (maybe larger). But that's fine, it shows gcd >= d. So the maximum gcd is at least d.
Conversely, if cost1(d)+cost2(d) > k, does that mean it's impossible to have both numbers multiples of d? Suppose we consider coordinated choices: maybe choosing a multiple for n1 that is not its nearest, and for n2 also not nearest, could lead to a lower total cost? No, because cost1(d) is the minimal distance to any multiple, and similarly for cost2(d). The sum of minima is <= the sum of distances for any pair of multiples. So if the sum of minima > k, then for any pair of multiples (a,b) we have |a-n1|+|b-n2| >= min_a |a-n1| + min_b |b-n2|? Actually careful: For a fixed d, the minimal sum of distances to some a (multiple of d) and b (multiple of d) is indeed the sum of the individual minima, because we can choose a and b independently. That is: min_{a in M_d} |a-n1| + min_{b in M_d} |b-n2| = cost1(d)+cost2(d). And this is the minimum total cost to get both numbers multiples of d. So if that exceeds k, then it's impossible. So condition cost1(d)+cost2(d) <= k is necessary and sufficient for existence of some pair (a,b) both multiples of d.
Thus the problem reduces to: find maximum d such that cost1(d)+cost2(d) <= k.
Now we need to compute cost1(d) quickly.
Given n1, n2 up to 1e5, k up to 100. d can be up to something like n1+n2+k? Actually a can be as high as n1+k, so d up to min(n1+k, n2+k) ≤ 1e5+100 ≈ 100100. So d up to around 100100. t <=5, so we could simply loop d from max_possible down to 1, compute cost1(d)+cost2(d) and stop at first satisfying. That would be O(max_n * t) ≈ 5 * 100100 = 500k, which is fine. However, we must compute cost1(d) efficiently for each d. cost1(d) = if d <= n1: min( n1 % d, d - n1 % d ) else: d - n1. But careful: For d <= n1, floor multiple exists and is >= d (since floor >=1). Ceil multiple might be n1 + (d - r) where r = n1%d. That's also >= d (since n1 >= d, ceil multiple at least d). So yes.
But is there any nuance with the decrement constraint for the case where we would decrement to a lower multiple? For n1 >= d, the floor multiple could be less than n1, which requires decrement operations. That's allowed as long as floor multiple >=1, which is true. The path to floor multiple involves decrementing step by step, which is allowed as explained. So no problem.
But what about the case where we choose the ceil multiple, which requires incrementing. That's allowed.
Thus cost1(d) as defined is the minimal number of operations to change n1 to a positive multiple of d.
So algorithm: For each test case, compute max_possible_d = min(n1 + k, n2 + k) maybe? Actually we could also consider d larger than both n1+k and n2+k? For d > n1+k, the nearest multiple is d (since n1 < d), cost1(d)= d - n1 > k, so sum > k because cost1(d) > k already. So d can't be > n1+k or > n2+k. So max_d = max(n1,n2)+k? But we need d such that both costs <= k individually? Not necessary: sum <= k, but if d > n1+k, cost1(d) = d - n1 > k, so sum > k. So d <= n1+k and d <= n2+k? Actually if d > n1+k, then cost1(d) > k, then sum > k even if cost2=0. So d must be <= n1+k and also <= n2+k? If d > n1+k but n2 is huge and cost2 maybe 0? but cost1>k => sum>k. So yes, d <= min(n1+k, n2+k). But also d could be as high as n1 + something? Actually if n2 is large, d could be up to n1+k (since cost1(d)=d-n1). But cost2(d) might be positive; the sum must be <= k. So d cannot exceed n1+k (otherwise cost1 > k). Similarly cannot exceed n2+k. So upper bound = min(n1+k, n2+k). That is about 100100.
We could also consider d up to 200000? But it's safe.
We can loop d from upper bound down to 1. For each d compute cost. But we can do better: Since k is small (<=100), the answer is likely near original numbers or large d. But brute-force is fine.
But careful: There might be a scenario where the nearest multiple for n1 is the floor, but floor multiple might be zero? For d > n1, floor multiple = 0 (invalid). So we treat that specially.
Implementation details: cost1(d):
if d <= n1:
r = n1 % d
return min(r, d - r)
else:
return d - n1
cost2 similarly.
Now check sum <= k.
But is there any scenario where we might want to set a or b to a value that is not a multiple of d but still achieve gcd exactly d? That doesn't make sense because if we want gcd(a,b) to be at least d, then both a and b must be divisible by d. So it's necessary.
Thus answer = max d such that cost1(d)+cost2(d) <= k.
But we need to consider that after adjusting to multiples, the gcd might be larger than d, but that's fine: it only ensures the existence of d.
So the algorithm is straightforward.
But wait, is there any other trick? Let's test with sample: n1=9, n2=10, k=1.
Compute for various d.
We want max d.
Upper bound min(9+1=10, 10+1=11) => 10.
Check d=10:
n1=9 < d => cost1 = 10-9=1.
n2=10 >= d? Actually n2=10, d=10 => d <= n2, r=10%10=0, cost2=min(0,10-0)=0.
total=1 <=1 => ok. So answer 10.
Matches sample.
Another test: n1=5, n2=7, k=0. Then we cannot change. So answer is gcd(5,7)=1. Check d=7? cost1=7-5=2>0, no. d=5: cost1=0 (r=0), cost2? 7%5=2, min(2,3)=2 >0? total=2 >0, not allowed. So max d maybe 1? d=1: cost1=0, cost2=0 => sum=0. So answer 1. That's correct.
Another test: n1=8, n2=12, k=2. gcd originally 4. Could we get 6? Check d=6:
n1=8: d<=8, r=8%6=2, min(2,4)=2
n2=12: r=12%6=0 => cost2=0 => total=2 <=2 => answer at least 6. Could we get 8? d=8:
n1=8%8=0 cost1=0
n2=12%8=4 min(4,4)=4 => total=4 >2 no.
d=7: n1:8%7=1 min(1,6)=1; n2:12%7=5 min(5,2)=2 total=3 >2 no.
d=6 seems max. Could we get 12? d=12: n1<12 cost1=12-8=4 >2. So answer 6.
Check manually: from 8 and 12, with 2 moves: can we get gcd=6? Possibly change 8 to 6? cost2? Actually we need both multiples of 6. Options: make 8 to 6 (cost 2 decrements), keep 12 (0), total 2 -> (6,12) gcd=6. Or 8 to 12 (cost 4) no. So 6 is possible. Good.
Another test: n1=1, n2=1, k=100. What's max gcd? Upper bound min(1+100,1+100)=101. Check d=101: cost1=101-1=100, cost2=100 total=200 >100. Not possible. Need sum <=100. For large d, cost ~ 2d -2. To be <=100, d roughly <=51? Actually we need d-1 + d-1 =2d-2 <=100 => d<=51. So answer 51. Check d=51: cost1=50, cost2=50 total=100. So can achieve (51,51) from (1,1) by incrementing both 50 times each? Wait, we have 100 ops: 50 increments to n1, 50 increments to n2 => total 100. So (51,51) gcd=51. Could we get 52? cost1=51, cost2=51 total=102 >100. So max 51. Good.
Now consider case where decrement needed: n1=100, n2=100, k=1. original gcd=100. Could we get 101? d=101: cost1=1 (101-100), cost2=1 total=2 >1. So not possible. d=100: cost1=0, cost2=0 total=0 <=1, so answer 100. So it's fine.
But what about d that is between n1 and n2? Works.
Now, is there any scenario where the minimal sum of distances to multiples is not achievable because of the decrement restriction? For n1 > d, the nearest multiple might be the floor, requiring decrements. But decrements are allowed as long as final a >=1. The floor multiple is at least d, so it's >=1, okay. However, what if the floor multiple is less than 1? That can't happen because floor multiple = d * floor(n1/d). Since floor(n1/d) >=1 because n1>=d, so >= d >=1. So fine.
If n1 = 1, and d=1, cost1 = 0. If d>1, we must increase to d, which is allowed.
Thus the approach seems correct.
But let's test a scenario where n1=2, n2=3, k=1. Could we achieve gcd=2? Check d=2:
n1=2 => cost1=0
n2=3: d<=3, r=3%2=1, min(1,1)=1 => total=1 <=1 => yes. So answer 2. Indeed, change n2 to 2 (decrement by 1) or to 4 (increment by1). Both cost 1, and (2,2) gcd=2 or (2,4) gcd=2. So ok.
What about n1=3, n2=5, k=2. Could we get gcd=5? d=5: n1=3<5 cost1=2, n2=5 cost2=0 total=2 -> yes, (5,5) gcd=5. So answer 5. Check d=6: cost1=3, cost2=1 total=4 >2 no. So max 5.
Now consider n1=6, n2=9, k=3. Original gcd=3. Could we get 7? d=7: n1=6 -> cost1=1 (to 7), n2=9: d<=9? Actually d=7 <=9, r=9%7=2, min(2,5)=2, total=3 -> yes. So (7,7?) Wait, to get multiples of 7: for n1 nearest is 7, for n2 nearest is 7? Actually 7 is not multiple of 7, 7 is multiple. For n2, nearest multiples: 7 (cost 2) or 14 (cost 5). So choose 7. So (7,7) gcd=7, cost 1+2=3. So answer 7. Could we get 8? d=8: n1:6 -> cost2? Actually n1<8 => cost1=2; n2=9: r=9%8=1 min(1,7)=1 total=3 -> (8,8) gcd=8? (8,8) gcd=8 cost 2+1=3. So answer 8? Check: n1:6 to 8 cost 2 increments; n2:9 to 8 cost 1 decrement. But decrement allowed from 9 to 8? Yes, because at each decrement, n2>1. So (8,8) gcd=8. So with 3 operations, we can achieve gcd=8. Could we get 9? d=9: n1=6 -> cost1=3 (to 9 or 0? actually nearest multiple: 9 (cost3) or 0? not allowed; floor multiple is 0? n1<9, floor is 0 invalid, so only 9, cost=3). n2=9 cost2=0 total=3 -> (9,9) gcd=9. Wait, can we get (9,9) from (6,9)? n1 to 9 costs 3 increments, n2 unchanged, total 3, so yes (9,9) gcd=9. That's even higher! So answer should be 9. So check: d=9 gives total cost 3, which is <=3. So max d is at least 9. Could we get 10? d=10: n1:6->4 (cost?), n1<10 => cost1=4, n2:9<10=> cost2=1? Actually n2<10 => cost2=1 (to 10) or to 0? only 10, cost=1, total=5 >3 no. So answer 9. So indeed we can achieve gcd up to 9. So our method would yield answer 9. Good.
Thus algorithm passes simple tests.
Now, is there any case where we might benefit from not using the nearest multiple for both? Example: We might want to set a and b to multiples that are not the individually nearest because maybe choosing a slightly farther multiple for one allows a much closer multiple for the other? No, because the choices are independent: the multiple for n1 does not affect the multiple for n2. So independent minimization yields the minimum sum. Indeed, min_{a in M, b in M} (|a-n1|+|b-n2|) = min_{a in M} |a-n1| + min_{b in M} |b-n2|. Because the sum separates. So yes.
Thus, straightforward.
But note: There is a nuance about decrement operations: we assumed we can always decrement to floor multiple if needed. However, what if the floor multiple is less than 1? Not possible as argued. But consider n1=1, and we want to go to a multiple of d where d > 1. The nearest multiple is d. That requires increments. That's fine.
Another nuance: The decrement operation condition is "if n1>1". So when we decrement step by step from n1 to a lower value, we must ensure that at each step the current value is >1. If at some step we go from 2 to 1, that's allowed because before the decrement n1=2 >1. After that, we stop. So fine. But what if we want to go from n1=2 to 0? Not allowed. So not an issue.
Thus algorithm stands.
But we need to be careful about time constraints: d can be up to 100100, t up to 5, so at most 500k iterations, each involving a few mod operations. That's fine for Python.
However, we might need to consider that mod operations can be expensive but 500k is okay.
But perhaps there is an even smarter method: since k is small (<=100), the answer is either original gcd or something near n1 or n2? But we can brute force.
But there is an edge case: d can be as high as n1 + n2 + k? No, because to have d > n1+k, cost1 > k alone. So maximum d is bounded by n1 + k, similarly n2 + k. So min of these.
But is it possible that by increasing both numbers, we can get a gcd larger than both? For example, n1=1, n2=2, k=5. Could we get gcd=4? Check: d=4: cost1=3 (to 4), cost2=2 (to 4), total=5 -> (4,4) gcd=4. So yes, answer 4. That's within bound: n1+k=6, n2+k=7, min=6, and 4 is within.
But what about n1=100000, n2=100000, k=100. Then max_d could be 100100. So we need to loop d from 100100 down to 1, that's 100k per test case, 5 cases => 500k, fine.
But we must be careful: for each d we compute mod operations. That's okay.
But there is one more nuance: The cost calculation for d > n1: cost1 = d - n1. However, if we increase n1 to d, we are making a = d. That is a multiple of d. But is it guaranteed that we can achieve a = d? Yes by incrementing. But if n1 is 1 and d is 100, we need 99 increments, that's allowed. So fine.
Similarly for n2.
Thus answer.
But I'm thinking: Could there be a scenario where we can achieve a gcd larger than the bound min(n1+k, n2+k) by using decrements? Example: n1=100, n2=1, k=100. The min(n1+k, n2+k) = min(200,101)=101. Could we get gcd=101? Check: d=101: cost1=1 (101-100), cost2=100 (101-1) total=101 >100, no. Could we get gcd=200? cost1=100 (200-100), cost2=199 >100? Actually cost2=199, sum >100. So no. So bound holds.
But could we get gcd=150? cost1=50, cost2=149, sum=199 >100. So not.
Thus bound is valid.
But is there any possibility to achieve a gcd > min(n1+k, n2+k) by decrementing the larger number? For example, n1=1000, n2=1000, k=0 => gcd=1000, bound=1000. So fine.
Now, we should consider that the answer might be very large if we can set both numbers to a common large value via increments. That value could be up to n1+k and also n2+k? Actually if we want both to be same number L, then cost = (L - n1)+(L - n2) if L >= both. So total = 2L - (n1+n2). For a given k, we can have L up to floor((n1+n2+k)/2). So maximum gcd could be up to that L. But L <= min(n1+k, n2+k) if n1 and n2 differ? Example: n1=1, n2=100, k=100. Then L = floor((1+100+100)/2)=100.5 floor 100. That's ≤ min(1+100=101, 200) = 101. So bound is still similar.
Thus iterative search is fine.
Now we must also consider that sometimes we might not need both numbers to be exactly multiples of d; we could have gcd = d even if both are not multiples of d? Actually if gcd(a,b) = d, then d divides both a and b. So necessary.
So algorithm:
For each test case:
read n1, n2, k.
max_d = min(n1 + k, n2 + k) # because if d > n1+k, cost1 > k alone
ans = 1
for d in range(max_d, 0, -1):
cost1 = ...
cost2 = ...
if cost1 + cost2 <= k:
ans = d
break
print(ans)
But we should also consider d maybe up to 10^5+100, but if k=0, max_d = min(n1, n2). So we can loop from min(n1,n2) downwards. However, we can just use max_d = min(n1+k, n2+k) always.
Now, compute cost efficiently:
def cost(n, d):
if d <= n:
r = n % d
return min(r, d - r)
else:
return d - n
But careful: For d <= n, r could be 0, then cost 0. d - r = d, but min(0, d)=0. So fine.
Potential issues: When d is large, mod operation may be expensive but okay.
But we can also compute using: cost = min(n % d, d - n % d) but mod yields remainder. That's O(1) per d.
Now consider edge case where d = 0? No.
Also consider d=1: cost1 = 0 always? For any n, d=1: if d<=n, r = n%1 =0, cost=0. If n<1? n>=1, so d=1 <= n always true (since n>=1). So cost1=0, cost2=0, sum=0 <= k always. So ans always at least 1. Good.
Case: n1=100000, n2=100000, k=100 => max_d = min(100100,100100)=100100. So loop from 100100 downwards. For d=100100: cost1 = d - n1 = 100, cost2=100 => total=200 >100, fail. So continue downwards. Eventually find d where sum <=100. The maximum d might be around 100000 + something? Actually let's compute approximate: For d > n1, cost1+ cost2 = 2d - (n1+n2). Set <= k => 2d <= n1+n2+k => d <= (n1+n2+k)/2. For n1=n2=100000, k=100, d <= (200000+100)/2 = 100050. So d max around 100050. For d near 100050, cost1+ cost2 = 2*100050 - 200000 = 100, so exactly. So d=100050 gives sum=100? Check: 100050 -100000 =50 each? Actually n1=100000, d=100050 => cost1=50, cost2=50 total=100. So yes. d=100051 => cost1=51, cost2=51 total=102 >100. So answer 100050. That is ≤ max_d=100100, so fine.
But is it possible that d=100050 is indeed less than min(n1+k, n2+k)=100100, so it's included.
Thus algorithm would find it.
Now, we must also consider scenario where d is less than both n1 and n2, but the nearest multiple for n1 might be the floor, which requires decrement, and for n2 might also be floor or ceil. The sum may be small.
Now, is there any scenario where the cost to go to the nearest multiple is not exactly the minimal number of operations due to intermediate restrictions? For decrement: we need to ensure that during decrement steps, we never go below 1. If we want to decrement n1 from value x to y where y>=1, the minimal number of steps is x-y (each decrement). But each decrement requires the current value >1. However, if y=1 and x>1, the last decrement goes from 2 to 1, which is allowed because 2>1. So okay. If y=0, not allowed. So as long as y>=1, it's fine.
But consider a scenario where we want to decrement n1 from 1 to something? That's not possible, but that's because y would be <1? Actually from 1 we cannot decrement because n1>1 condition fails. But if y=1, then no decrement needed. So fine.
Thus cost function correct.
Now, one more nuance: The operations are allowed in any order and we can intermix. Could there be a scenario where we can achieve a pair (a,b) with total operations less than sum of absolute differences because we can use an operation that changes both? No, operations affect only one variable at a time. So the minimal number of operations to change n1 to a and n2 to b is exactly |a-n1|+|b-n2|, provided all intermediate steps are valid. Since we can always apply all changes to n1 first then to n2, or interleaved, the total count is the sum of absolute differences. The only possible issue is if while changing n1 we might need to temporarily go to a value that violates the decrement condition? But we can always perform operations in an order that avoids illegal states: For increments, no restriction. For decrements, as long as we are decrementing from a higher value to a lower, each step is allowed if the current value >1. Since we start from n1 (>=1) and target a (>=1). If a <= n1, we are just decrementing stepwise. In that process, we never go below a. The only problem could be if at some point the value becomes 1 and we attempt another decrement. But if a >=1, we stop at a. If a=1 and n1 >1, the last decrement from 2 to 1 is allowed, then we stop; we don't decrement further. So fine.
Thus minimal total operations = Manhattan distance in 1D independent moves.
Thus condition stands.
So final solution is straightforward.
But before finalizing, let's test more edge cases:
n1=2, n2=3, k=0 => answer gcd=1. cost1+ cost2 for d=1 is 0. So answer 1.
n1=2, n2=3, k=1 => answer 2 as earlier.
n1=10, n2=10, k=5 => answer? We can get d=15? cost1=5, cost2=5 total=10 >5, no. d=12? cost1=2, cost2=2 total=4 <=5 => gcd at least 12. But can we get 13? cost1=3, cost2=3 total=6 >5. So answer 12? But maybe 14? cost1=4, cost2=4 total=8 >5. So 12 seems max. But can we get gcd=14 by making numbers 14 and 14? That would cost 4+4=8 >5. So no. Could we get gcd=15 as earlier cost 5+5=10. So answer 12. But is there a way to get gcd=13 with total ops <=5? For d=13, nearest multiples: n1=10 -> multiple 13 (cost3) or 0? only 13, cost=3; n2=10 -> cost=3; total=6 >5. Could we do something like (13,26) with gcd 13? For n2=10 to 26 cost 16, too high. So no. So answer 12. But is there a possibility to get gcd=12 but not both numbers exactly 12? (12,24) cost for n2=10 to 24 =14 >5. So only both 12 works. So 12 is correct.
But is there any chance to get gcd=14 with total ops 5 by making numbers 14 and 28? That's 4+18 >5. So no.
Thus algorithm would give d=12.
Now test n1=100000, n2=99999, k=100. Bound min(100100, 100099)=100099. We'll compute answer. Should be around (n1+n2+k)/2 = (199999+100)/2=100049.5 => floor 100049. Let's approximate: d=100049 => cost1=49, cost2=50 => total 99 <=100. d=100050 => cost1=50, cost2=51 => total 101 >100. So answer 100049. That is < max_d, so fine.
Thus algorithm works.
Now, but wait: There might be a scenario where the optimal d is not covered by our cost function because we might want to set a to some multiple of d and b to some multiple of d, but the minimal sum of distances is cost1(d)+cost2(d). That's correct.
But is it possible to achieve a gcd larger than d by using a pair that are not both multiples of d but whose gcd is > d? For example, if we find that for d=6 the sum <= k, we know gcd >=6 possible. But we want the maximum possible gcd. So if we find a d such that sum <=k, we know the answer is at least d. So to find the maximum, we just need the largest d satisfying that. So it's sufficient.
Thus answer = max d such that cost1(d)+cost2(d) <= k.
Now, implement.
But there is a catch: The cost for d when n1 >= d is min(r, d-r). But this is the distance to the nearest multiple. However, is it always possible to achieve that nearest multiple? For example, suppose n1=3, d=2. Then r=1, d-r=1, so cost1=1. The nearest multiples are 2 and 4. To get to 2, we need to decrement by 1 (3->2). That's allowed. To get to 4, increment by 1, allowed. So yes.
Now, any scenario where the nearest multiple is the floor, but the floor multiple is 0? That would happen if n1 < d, but floor(n1/d)=0, giving a=0, not allowed. In such case we only have ceil multiple (d). Our cost function returns d - n1, which is the distance to ceil multiple. So it's fine.
Now, the cost function doesn't consider the possibility of using multiples like 2d, 3d for n1 when n1 < d? For n1=2, d=5, nearest multiple could be 5 (cost 3) or 10 (cost 8) or 0 (invalid). So 5 is nearest. So cost=3. That matches d - n1 =3.
Now, check case n1=100, d=150: cost1=50 (to 150). Could we do better by going to 0? No. So fine.
Now, check case n1=100, d=101: cost1=1. Could we go to 202? cost=102 >1. So no.
Thus correct.
Now, implementation details: t up to 5. For each test case, we read three lines: n1, n2, k. In sample input, they give each on a separate line, but the problem statement says each test case contains 3 lines with inputs n1, n2, k respectively on each line. So we can read t, then for each test case, read n1 as integer, then n2, then k.
Now, we need to output max possible gcd.
Edge Cases: k might be 0. Then answer is gcd(n1, n2)? Not necessarily, because if we can't change, gcd is gcd(n1,n2). But our method will check d from min(n1,n2) down. For d = gcd(n1,n2), cost1(d)+cost2(d) = 0? Actually for d = gcd(n1,n2), both n1 and n2 are multiples of d, so cost1=0, cost2=0, sum=0 <= k. So answer is at least gcd. For d > gcd, at least one of n1,n2 is not a multiple, so cost >0. Could it be that d > gcd is still achievable with 0 operations? Only if both are already multiples, i.e., d divides both. That would imply d divides gcd. So d cannot be larger than gcd. So the maximum d with sum 0 is exactly gcd. So our loop descending from min(n1,n2) will first encounter gcd? Not necessarily, because there could be a d > gcd that also yields sum 0? That would require both n1 and n2 multiples of d, which means d divides both, thus d <= gcd. So no. So it's fine.
But if k>0, we might get larger than gcd.
Now, complexity: For each d, we compute two mod operations. 100k * 5 = 500k mod operations, which is trivial.
But is there any scenario where d can be as large as 200000? Suppose n1=100000, n2=100000, k=100000? But k is limited to 100. So d <= 100100. So fine.
But we should also consider that the bound min(n1+k, n2+k) might be less than the actual max possible d? Let's consider a scenario: n1=100000, n2=1, k=100. min(100100, 101) = 101. So we only check d up to 101. But could we achieve a gcd > 101? For d=150, cost1=50 (to 150) but cost2=149 (to 150) total 199 >100. Could we achieve a gcd of 150 by making numbers (150, 150)? sum=149+149? Actually n2=1 to 150 cost 149, n1 to 150 cost 50 total 199 >100. Could we make numbers (150, 300) gcd=150? cost1=50, cost2=299 => 349 >100. So impossible. So max d is indeed limited by the smaller of n2+k? Actually n2+k = 101, so d cannot exceed 101 because if d > 101, cost2 >= d - 1 > 100, so total >100. So bound is correct. So using min(n1+k, n2+k) is safe.
Now, could there be a scenario where we could achieve a d greater than n2+k by decreasing n2? That is, maybe we can decrement n2 to a multiple of d that is less than n2, so cost2 = n2 - (some multiple). But that multiple would be at most n2 (since decrement). So if d > n2, the only multiples are at least d, which are > n2. So you cannot get a multiple less than d because the only positive multiples are d, 2d, ... all > n2. So you would have to increase. So cost2 = d - n2 (if you go to d) or 2d - n2, etc, but the smallest distance is d - n2 (since d > n2). So cost2 = d - n2, which > k if d > n2+k. So bound stands.
Thus the algorithm is correct.
Now, before coding, let's test with some tricky cases.
Case: n1=1, n2=100000, k=100. We'll compute:
max_d = min(1+100=101, 100000+100=100100) = 101.
Loop d from 101 down:
d=101: cost1 = 100 (101-1), cost2 = 100000-? Actually n2=100000, d=101 <= n2, so r = 100000 % 101. 100000 mod 101 = ? 101*990 = 99990, remainder 10; d - r = 91. So min=10. So total = 100+10 =110 >100. Not good.
d=100: cost1 = 99 (100-1), cost2: 100000%100=0, min=0 => total=99 <=100? 99 <=100 yes. So d=100 works. So answer 100.
Is that achievable? n1 from 1 to 100 costs 99 increments; n2 stays 100000, which is multiple of 100. So gcd(100,100000)=100. Could we get 101? cost1=100, cost2=10 total 110 >100. So 100 is max.
Thus answer 100.
Now, what about n1=1, n2=99999, k=100. max_d=101. d=101: cost1=100, cost2: 99999%101. 101990=99990? Actually 101990=99990, remainder 9; d-r=92; min=9 => total=109>100. d=100: cost1=99, cost2: 99999%100=99, min(99,1)=1? Actually 100-99=1, so min=1 => total=100 <=100? 99+1=100 yes. So d=100 works. Could d=101? no. So answer 100.
Check d=102: cost1: n1=100, d<=100? Actually d=102 > 100, so cost1=2 (102-100). cost2: n2=102, cost2=0 => total=2 <=3 => so d=102 achievable. Could we get d=103? cost1=3, cost2=1 (103-102) total=4 >3. So answer 102.
But maybe we could get d=104? cost1=4, cost2=2 total=6 >3. So answer 102.
Now, consider scenario where both numbers are close but one is slightly less than the other. Works.
Now, let's think about if the minimal sum of distances to multiples is indeed the minimal total operations. There is a nuance: The operations are performed sequentially, and we have a global limit on total operations. But we can apply operations to both numbers interleaved. However, the total count is additive.
But is there any scenario where we might need extra operations because of the restriction that decrement only allowed if >1? Suppose we want to change n1 from 2 to 1. That's one decrement (2>1). So cost=1. That's fine. But what if we want to change n1 from 1 to 2? That's increment, cost=1. So ok.
Now, consider a scenario where we want to change n1 from 1 to 0? Not allowed and not needed.
Thus fine.
Now, we need to consider the possibility of using operations that change both numbers simultaneously? No, only single number.
Thus solution is set.
Now, we must also consider that the gcd could be made larger by making one number very large and the other also large but not equal, but sharing a large common divisor. However, our cost1(d)+cost2(d) captures the ability to make both multiples of d. The actual gcd might be a multiple of d, but that's okay because we only need to know if we can achieve at least d. For maximum gcd, we find the largest d for which we can make both numbers multiples of d. Since if we can make both multiples of d, gcd(a,b) is at least d; it could be larger, but that larger value would also satisfy the condition for that larger d (if we can achieve it). Actually if we achieve a,b that are multiples of d, their gcd might be some d' > d. Then for d' we could also claim that there exists a,b that are multiples of d'? Wait, if gcd(a,b)=d', then a and b are multiples of d' by definition. So if we achieve a,b with gcd d', then d' also satisfies the condition. So the maximum d satisfying condition is exactly the maximum achievable gcd. Because if G is the maximum gcd, then condition holds for d=G, and for any d > G, condition fails. Also there may be d < G that also satisfy, but we are looking for max. So yes.
Thus answer = max d with cost1(d)+cost2(d) <= k.
Now, we could also try to compute directly using some math: Since k is small, we could try to enumerate possibilities of adjusting numbers by small amounts and compute gcd. But our method is simpler.
Implementation:
Read t.
For each test case:
n1 = int(input())
n2 = int(input())
k = int(input())
max_d = min(n1 + k, n2 + k)
ans = 1
for d in range(max_d, 0, -1):
# compute cost1
if d <= n1:
r = n1 % d
cost1 = min(r, d - r)
else:
cost1 = d - n1
if d <= n2:
r = n2 % d
cost2 = min(r, d - r)
else:
cost2 = d - n2
if cost1 + cost2 <= k:
ans = d
break
print(ans)
But we should also consider that we might break early.
Now, we should consider the possibility that the maximum d might be greater than max_d? As argued no.
But what about d = 0? Not relevant.
Now, performance: For d from 100k down to 1, worst-case 100k iterations, each with two mods. That's fine.
But we could further optimize: Since k is small, the answer is either near n1 or n2? But not necessary. However, we can also consider that the cost function is piecewise, and we could compute candidate d's around multiples of n1 and n2? But it's okay.
Now, consider a scenario: n1=10^5, n2=10^5, k=100. max_d=100100. That's fine.
But maybe we can reduce the loop range: We know answer is at least max(gcd, something). But it's okay.
Potential pitfalls: When d is large, n1 % d for d > n1? Actually if d > n1, d <= n1 is false, so we go to else branch. So we avoid mod for d > n1. Good.
Now, test with n1=5, n2=5, k=1: max_d = min(6,6)=6. Loop d=6: cost1=1, cost2=1 total=2 >1; d=5: cost1=0, cost2=0 total=0 <=1 => ans=5. So answer 5, which is gcd(5,5)=5, unchanged. But could we get 6? No.
Now, test with n1=5, n2=5, k=2: d=6: cost1=1, cost2=1 total=2 <=2 => ans=6. So answer 6. Check: (6,6) gcd=6, cost=2.
Now, consider n1=5, n2=5, k=3: d=6: 2 ops, but maybe d=7? cost1=2, cost2=2 total=4 >3; d=8? cost1=3, cost2=3 total=6 >3. So answer 6? But d=7? no. d=6 works. Could we get d=8 with only 3 ops? (8,8) cost 3+3=6 >3; (8,4) gcd 4, not 8; (8,16) cost 3+11 >3. So 6 is max. So fine.
Now, test with n1=5, n2=7, k=4. d=8? n1<8 cost1=3, n2<8 cost2=1 total=4 <=4 => ans=8. Check (8,8) cost 3+1=4, gcd=8. Could we get 9? cost1=4, cost2=2 total=6 >4. So answer 8.
Now, test n1=5, n2=7, k=5: d=9? cost1=4, cost2=2 total=6 >5; d=8 works with 4, so answer 8? But maybe d=10? cost1=5, cost2=3 total=8 >5. d=9? no. So 8. But could we get gcd=9 by making numbers 9 and 9? cost1=4, cost2=2 total=6 >5; 9 and 18? cost1=4, cost2=11 total=15; no. So 8. However, could we get gcd=10? cost1=5, cost2=3 total=8 >5. So 8.
Now, test n1=5, n2=7, k=6: d=9: total=6 <=6 => ans=9. So answer 9.
Now, test n1=5, n2=7, k=7: d=10? total=5+3=8 >7; d=9 total=6 <=7 => ans=9; but maybe d=11? cost1=6, cost2=4 total=10 >7; d=12? 7+5=12 >7; So 9. So answer 9.
Now, test n1=5, n2=7, k=8: d=10 total=8 <=8 => ans=10. So answer 10. So with 8 ops we can achieve gcd 10 (by making both 10? cost1=5, cost2=3 total=8). So yes.
Thus algorithm seems consistent.
Now, think about the possibility that we can get a gcd that is not a divisor of either original number? That's fine, as long as we can adjust.
Now, there is one more nuance: The operation count includes both increments and decrements. Our cost function uses absolute differences. But consider the scenario where we need to decrement but the current value is 1, we cannot decrement further. But if we want to go from n1=1 to a=0 (not allowed) we wouldn't. So cost is always non-negative.
Now, but what about a scenario where we want to go from n1=1 to a=2? That's increment, cost 1. So fine.
Now, could there be a scenario where we want to go from n1=2 to a=1, but we also want to go from n2= something to something else, and we might intersperse operations to avoid having to decrement from 1? Not relevant.
Thus all good.
Now, we should also consider that the cost to get to a multiple d could be lower if we choose a multiple that is not the nearest but requires less total operations due to some combined move? For example, suppose n1=4, n2=6, d=5. Nearest multiples: for n1: 5 (cost1=1) or 0? Actually floor multiple 0 invalid, so only 5, cost1=1. For n2: 5 (cost2=1) or 10 (cost2=4). So minimal sum = 2. But could we make a=10, b=10? That would cost 6+4=10, higher. So minimal sum is 2.
But what if we consider making a=5, b=10? cost1=1, cost2=4 total=5 >2. So no.
Thus independent minima works.
Now, consider the possibility that we could use the same operation to affect both numbers? No.
Thus correct.
Now, let's think about extreme cases:
Case: n1=100000, n2=1, k=100. We already did: answer 100.
We want max d such that 2*(d-100000) <=100 => d <=100000+50 =100050. So answer 100050.
Check d=100050: cost1=50, cost2=50 total=100. So yes.
But is it possible that d=100051? cost1=51, cost2=51 total=102 >100. So answer 100050.
But note: d must be integer. So ok.
Now, could we achieve d=100050 with exactly 100 ops? Yes, increment both by 50 each.
Now, does our cost calculation for d=100050 yield cost1 = min(r, d-r) for n1=100000? Since d <= n1? Actually d=100050 > n1, so cost1 = d - n1 = 50. So correct.
Now, if we try to see if we could achieve a gcd larger than 100050 by making one number larger than the other? Suppose we set a=100050, b=200100? Then b must be multiple of d, so b could be 100050*2=200100, cost for b = 200100-100000=100100 >100. Not good.
Could we set a=100050, b=100050? That's fine.
So answer 100050.
Thus algorithm works.
Now, but we must check if there is any scenario where d > min(n1+k, n2+k) could be possible because we could decrease one number to a value that is a multiple of d but d is larger than the original? For example, n1=100, n2=100, k=100. Could we achieve d=150? For n1, to become multiple of 150, we could go to 0? Not allowed. To 150, cost 50. To 300, cost 200. So best is 150. So cost1=50. Similarly cost2=50 => total=100. So d=150 works? Check: min(n1+k, n2+k)=200. So 150 <= 200, so within bound. So not an issue.
But what if n1=100, n2=1, k=100. Could we achieve d=150? cost1=50, cost2=149 total=199 >100. So no.
Thus bound min(n1+k, n2+k) is a safe upper bound, but not necessarily the maximum possible d? Actually maximum d cannot exceed min(n1+k, n2+k) because if d > n2+k, cost2 > k, so total > k. So yes, maximum d <= min(n1+k, n2+k). So using that as loop start is correct.
Now, we can also compute answer directly using some formula? Not needed.
Now, before coding, think about potential integer overflow? Not in Python.
Now, let's consider time: t=5, max_d up to 100100, so 500k iterations, each doing a few operations. It's fine.
But wait: n1 and n2 up to 1e5, but k up to 100, so max_d up to 100100. That's fine.
But what about t=5 and n1=n2=1e5, k=100, then max_d=100100. That's 100100 iterations per test, times 5 = 500500. That's fine.
Now, we must be careful to read input correctly. The sample input shows:
1
9
10
1
So they give t, then for each test case three lines: n1, n2, k. So we read accordingly.
Now, we can implement solve().
But I recall a similar problem on platforms like "Maximum GCD after operations" etc. This approach is standard.
But wait: There is another nuance: The operations can be performed at most k times. So we can also choose to use fewer than k operations. Our condition cost1+ cost2 <= k allows using less or equal.
Now, what about the possibility of using more than k operations? Not allowed.
Now, we should also consider that we could make a=0? But a must be >=1 because decrement only allowed if >1, so a can't be 0. Also gcd(0,b) is b, but b could be >0. However, is setting a=0 allowed? For a to be 0, we would need to decrement from 1 to 0, which is not allowed because when n1=1, cannot decrement. So a cannot become 0. Similarly b cannot become 0. So a,b >=1. So our multiples must be >=1. That's satisfied.
But could we set a=0 by increments? No, increments only increase. So no.
Thus domain is positive integers.
Now, is there any scenario where it's beneficial to set one number to 0 to get high gcd? For example, a=0, b=100, gcd(0,100)=100. So if we could set a=0, we could achieve gcd=100 with cost: from n1=1 to 0? Not allowed. So not possible.
Thus ignore.
Now, double-check decrement condition: "Decrement n1 by 1(if n1>1)". That means if n1=1, you cannot decrement. So you cannot go to 0. So we are safe.
Now, final answer.
But one more nuance: In the cost function, for n1 >= d, we considered both floor and ceil multiples. However, if the floor multiple is 0? Not possible because n1 >= d implies floor(n1/d) >=1, so floor multiple >= d >=1. So fine.
Now, could the ceil multiple be such that it's less than 1? No, ceil multiple = d * ceil(n1/d). Since n1>=1, ceil(n1/d) >=1. So >= d >=1.
Thus both are valid.
Now, there is an edge case: d = 0? No.
Now, also note that the problem asks for maximum possible value of gcd(n1,n2). Not necessarily that we must use exactly k operations, but at most k.
Thus condition <= k.
Now, code.
But wait, we need to consider that the answer could be larger than min(n1, n2) if we increase both numbers? Yes.
Now, there is a possibility that the maximum d is not found by our loop from max_d downwards because the condition might hold for a d > max_d? But we argued it's impossible. Let's prove: Suppose d > n1 + k. Then cost1(d) >= d - n1 > k (since d - n1 > k). So total cost >= cost1 > k, so condition fails. Similarly if d > n2 + k. So any d satisfying condition must be <= n1 + k and <= n2 + k, thus <= min(n1+k, n2+k). So our loop covers all possible d.
Now, we can implement.
But before finalizing, think about potential micro-optimizations: Since k is small, we could limit the search to d values that are within k of n1 or n2 or something. But not needed.
Now, test with random values to ensure correctness.
We'll implement a quick mental test: t=1, n1=10, n2=20, k=5.
Compute max_d = min(15,25) = 15.
Loop d=15: cost1 = 5 (15-10), cost2 =? n2=20, d=15 <=20, r=20%15=5, d-r=10, min=5 => total=10 >5, no.
d=14: cost1=4, n2%14=6, d-r=8 min=6 => total=10, no.
d=13: cost1=3, n2%13=7, d-r=6 min=6? Actually 13-7=6, min=6 => total=9.
d=12: cost1=2, n2%12=8, d-r=4 min=4 => total=6.
d=11: cost1=1, n2%11=9, d-r=2 min=2 => total=3 <=5 => ans=11. So answer 11.
Check: from (10,20) to (11,22)? Actually to make both multiples of 11: n1 to 11 cost1=1, n2 to 22 cost2=2 (or to 11 cost2=9). Best is 22 (cost2=2). total=3. gcd(11,22)=11. Could we get 12? d=12 cost total=6 >5. So 11 is max. Good.
Now, try n1=10, n2=20, k=7: d=13 total=9 >7? Actually d=13 total=9 >7, d=12 total=6 <=7, so ans=12? But maybe d=14 total=10 >7; d=15 total=10; So 12. But could we get 14 with total 7? For d=14, cost1=4, cost2 = min(20%14=6, 8)=6, total=10. So no. So 12.
But maybe d=13 with a different multiple? For n1=10, best multiple of 13 is 13 cost 3. For n2=20, could we choose 26? cost 6 => total 9. Or 13 cost 7 => total 10. So min is 9. So indeed.
Thus okay.
Now, consider n1=1, n2=1, k=100: max_d=101. We'll compute answer=51 as earlier. Let's simulate: d=101 cost1=100, cost2=100 total=200 >100; d=100 cost1=99, cost2=99 total=198 >100; ... down to d=51: cost1=50, cost2=50 total=100 <=100; d=52: cost1=51, cost2=51 total=102 >100. So answer 51.
Now, could there be d=50? cost1=49, cost2=49 total=98 <=100, but we want max, so we need to find the highest. So algorithm yields 51.
Now, we have to ensure that loop goes from max_d downwards, and we break at first d satisfying condition. So we need to compute max_d properly. However, consider the possibility that max_d may be less than the actual answer? Not possible. But if max_d is too low, we might miss. So we must be absolutely sure about bound. Let's test a borderline case: n1=100, n2=50, k=100. min(n1+k, n2+k) = min(200,150)=150. Could answer be >150? Suppose we try d=160. cost1=60 (160-100), cost2=110 (160-50) total=170 >100. So no. So bound holds.
Now, what about scenario where we can achieve gcd by decreasing the larger number to a multiple that is less than it? For d > n2, the only multiples are >= d > n2, so we can't decrease. So bound stands.
Thus we are good.
Now, consider scenario where n1=2, n2=3, k=100. max_d = min(102,103)=102. But answer? We can achieve d up to around (2+3+100)/2 = 52.5, so 52? Let's compute: for d=52, cost1=50, cost2=49 total=99 <=100; d=53: cost1=51, cost2=50 total=101 >100. So answer 52. That's <=102, so loop from 102 down will find 53? Actually check d=53 fails, d=52 succeeds, so answer 52. That is < max_d, so fine.
Now, think about the possibility that the answer is greater than max_d? Suppose n1=100000, n2=1, k=100000? But k max is 100, so irrelevant.
Thus implementation ready.
Now, one more nuance: When we compute cost for d <= n1, we do min(r, d - r). But careful: In Python, % gives remainder in [0, d-1]. So r is in [0, d-1]. d - r is in [1, d] (if r=0, d-r = d). But is d - r a valid distance to the next multiple? The next multiple is (n1//d + 1)*d. The distance to that is d - r. However, if r = 0, then the distance to the next multiple is d, but the distance to the previous multiple (which is n1 itself) is 0. So min(0, d) = 0, correct. So that works.
Now, what about the case when d - r might be larger than something? It's fine.
Now, possible micro-optimization: For d > n1, we use d - n1, which is correct.
Thus code is straightforward.
But before final answer, let's think if there could be a scenario where the optimal gcd is achieved by making a and b such that they are not both the nearest multiple individually, but the sum of distances is less than the sum of the minima? That can't happen because minima are per variable. The sum of minima is the minimum possible sum because you can independently choose the best for each. So it's the true minimum.
However, there is a subtlety: The cost for n1 to reach a multiple of d might not be exactly the distance if we are not allowed to decrement to that multiple due to the >1 condition? But we already considered that. But what if the floor multiple is less than 1? Not possible. But what if the floor multiple is 1 and n1=2? That's allowed. But what if n1=2, d=2? floor multiple=2, cost 0. fine.
Now, what about n1=2, d=3: The nearest multiple is 3, cost1=1 (increment). That's allowed. If we wanted to go to 0, not allowed. So fine.
Thus all good.
Now, is there any scenario where we might want to use a combination where we change one number and then the other, and the operations on one affect the ability to change the other? No.
Thus solution stands.
But wait, I recall a problem "Maximum GCD after at most K operations" on CodeChef perhaps. The solution described often involves trying all possible GCD values from 1 to something, or using the fact that K is small to try adjusting numbers within ±K. Actually there is a typical problem: given two numbers, you can increase or decrease by 1, maximize gcd after at most K operations. And solution often is: try all divisors of numbers around original ±K. But here we have both numbers up to 1e5 and K up to 100, so we could also enumerate all possibilities of new values a in [n1-k, n1+k] and b in [n2-k, n2+k]? But that would be O(k^2) = 10^4 per test, also fine. However, we might miss cases where we increase beyond n1+k because maybe it's beneficial to increase a lot? Actually if we increase a lot, it could increase gcd more. For example, n1=1, n2=1, k=100, increasing both to 51 yields gcd 51, which is >1+100? Actually 51 is within [1-100,1+100]? That's [ -99, 101], but a=51 is within range [1,101] if we consider non-negative. But if we only consider range n1±k, we would only consider a up to 101, which includes 51, so fine. But what about n1=100000, n2=100000, k=100, the optimal a=100050 is within [99900,100100]? Actually 100050 is within 100000±100, yes (100050 = 100000+50). So it's within that range. But consider n1=100000, n2=1, k=100, optimal a=100, b=100000? Actually answer 100, a=100 (within [99900,100100]? Actually 100 is far less than 99900? n1=100000, n1 - k = 99900, so a=100 is not within that range. So if we only consider a in [n1-k, n1+k], we would miss a=100 because 100 is far below. But we could also consider decreasing n1? However, we can decrement n1 to 100? That would be 100000-100 = 99900? Wait, from 100000 to 100 would require 99900 decrements, which is >k. So not possible. In the optimal solution for n1=100000, n2=1, k=100, we set a=100 (increase from 1) and keep n1=100000? Actually a is n1? We set n1 unchanged? Wait, in that case, to achieve gcd=100, we can keep n1=100000 (which is multiple of 100), and increase n2 from 1 to 100 (cost 99). So a = n1 = 100000, which is within [n1-k, n1+k] because 100000 is exactly n1. So a is within range. b=100 is n2 increased to 100, which is within [n2-k, n2+k]? n2=1, n2+k=101, so 100 is within [1-100? Actually lower bound is 1 (since cannot go below 1) but our range would be [1, 101] effectively. So b is within [n2, n2+k] (since we only increase). So both a and b are within [original - k, original + k] after considering decrements only down to 1? Actually for n2, the minimal value is 1, so possible b values are from 1 to n2+k (since we can increment up to n2+k, and decrement down to 1 if n2>1). So range length roughly k+ n2? For n2=1, we can only increase up to 1+k, and cannot decrease below 1. So range length is k+1. For n1=100000, we can decrease down to max(1, n1-k) but n1-k = 99900, so range is [99900, 100100] (if we consider decrement limit). So a=100000 lies in that range. So both optimal values lie within the set of values reachable with at most k ops individually. But is it always true that the optimal a and b lie within [n_i - k, n_i + k]? For a number, the minimal number of operations to change it to a is |a - n_i|. If |a - n_i| > k, then it would exceed k total unless the other uses negative? But total sum must be <= k, so each individual |a - n_i| <= k? Not necessarily; one could be >k if the other is negative? But both are non-negative, so sum <= k implies each <= k. Because if |a - n1| > k, then even if |b - n2| = 0, sum > k. So indeed each individual distance must be <= k, because both are non-negative. So |a - n1| <= k and |b - n2| <= k. Thus a ∈ [n1 - k, n1 + k] but also a ≥ 1. So a is in that interval intersected with [1, ∞). Similarly for b. So indeed the search space for a and b is limited to intervals of length 2k+1 (up to 201). Since k ≤ 100, that's at most 201 values each, product 40401. So we could also simply enumerate all possible a and b within reachable ranges and compute gcd, then take max. That might be simpler to implement and ensure correctness. But we must be careful: For each a, we need to ensure it's reachable with at most k operations, but that's exactly if |a - n1| <= k and also if a < n1, we must ensure we can decrement to a. But as argued, if a is between 1 and n1, it's reachable by decrements as long as a >=1. And if a > n1, reachable by increments. So the only restriction is a >= 1. So the set of reachable a is: a ∈ [max(1, n1 - k), n1 + k] integer. Similarly for b. Then we can simply iterate over all a in that range, all b in that range, compute gcd(a,b), and track max. That would be O((2k+1)^2) per test, which is at most 201^2 = 40401, times t=5 = 202k, trivial. This is even simpler and avoids the need to reason about d and cost functions. Moreover, it directly uses the constraints and avoids any off-by-one in cost function.
But wait, is it always sufficient to consider a and b only within those ranges? Because total ops sum <= k, so each individual |a-n1| <= k? Not necessarily: it's possible that |a-n1| > k if |b-n2| is negative? But distances are absolute, non-negative. So sum <= k implies each term <= k because if one term > k, sum > k (since other term >=0). So yes, each must be <= k. So indeed a must satisfy |a-n1| <= k and a >=1. So a ∈ [max(1, n1 - k), n1 + k]. Similarly for b. So we can enumerate.
But is there any scenario where the optimal solution uses a value outside that range because we can use more operations on one and fewer on the other such that total <= k, but individually each could be > k? Example: suppose k=5, n1=10, we could change n1 to 16 (cost 6) and n2 to 1 (cost 9)? That sum would be 15 >5. So no. If we try to set a=16 (cost 6) and b=9 (cost 1) sum=7 >5. So at least one >k leads sum >k unless the other is negative (impossible). Because both terms are non-negative, the sum <=k forces each ≤ k. So indeed both distances individually ≤ k.
Thus the enumeration approach is valid and simpler. However, we must consider that the interval [n1 - k, n1 + k] may include values less than 1. So we intersect with [1, ∞). So a_min = max(1, n1 - k), a_max = n1 + k. Similarly for b. Then we can iterate over all a in that range, all b in that range, compute gcd, keep max. But is this always enough? Consider that we might use fewer than k operations total, but we only need at most k. So considering all a,b within those ranges covers all possibilities because if a is outside that range, then |a-n1| > k, so cannot be used. So yes.
Now, what about the decrement restriction? For a in [max(1, n1 - k), n1] (i.e., decreasing), we need to ensure that we can decrement stepwise from n1 to a. That's allowed as long as a >=1. The only potential issue is if a=0, but we have a_min >=1, so fine. For a > n1, we increment, always allowed. So all a in that range are reachable.
Thus enumeration is straightforward and avoids any mathematical derivation.
But wait, is it possible that the optimal solution uses a value a that is less than n1 - k but still total operations <= k because we use negative operations on b? No, because total ops = |a-n1|+|b-n2|, if |a-n1| > k, even if |b-n2| = 0, sum > k. So not allowed.
Thus enumeration of a and b within the ranges is exhaustive.
Now, complexity: For each test case, we have up to (2k+1)^2 pairs, where k ≤ 100, so at most 201*201=40401. For t=5, about 200k gcd computations. gcd is O(log min(a,b)), very fast. So this is also efficient and simpler.
But is there any catch? The ranges could be large if n1 is small and k is large? For n1=1, a_min = max(1, 1 - k) = 1, a_max = 1 + k = up to 101. So range size = k+1 ≤ 101. For n1=100000, a_min = max(1, 100000 - 100) = 99900, a_max = 100100, range size = 201. So maximum range size is 2k+1 = 201 when n1 > k. So total pairs at most 201*201 = 40401. So that's fine.
Thus we can implement this approach easily. It avoids potential off-by-one in cost function and is more direct.
But note: The enumeration approach will consider all pairs (a,b) within the ranges, compute gcd, and take max. However, we must ensure that we consider only pairs that are reachable with total operations <= k. But if we restrict a and b to their individual ranges, it's possible that for some a,b, the sum |a-n1|+|b-n2| > k? Because if a is at the far end of its range (say n1 + k) and b is at the far opposite end (say n2 - k) but note b's range lower bound might be > n2 - k? Actually b_min = max(1, n2 - k). If n2 - k >=1, then b_min = n2 - k. So b could be n2 - k. Then total ops = k + k = 2k, which could be > k if k>0. So indeed, restricting individually ensures each distance ≤ k, but the sum may exceed k. For example, n1=100, n2=100, k=10. a=110 (cost 10), b=90 (cost 10) gives total 20 > 10. So such a pair would be considered in enumeration but would exceed total ops. So we must also check that |a-n1|+|b-n2| <= k. So we need to filter for total cost.
Thus enumeration would be: for a in range(a_min, a_max+1):
for b in range(b_min, b_max+1):
if abs(a-n1) + abs(b-n2) <= k:
ans = max(ans, gcd(a,b))
That's still O(201^2) per test case, which is fine.
Alternatively, we could compute cost for each candidate d as before, which also O(max_d). Both are fine. The enumeration may be simpler to code correctly, with less chance of logical error. However, we need to be careful about the ranges: a_max = n1 + k, a_min = max(1, n1 - k). But is it always true that we can reach a value a = n1 - k if n1 - k >=1? Yes, by decrementing k times. But note: decrement operation condition: each decrement requires current >1. When we decrement from n1 down to n1 - k, we need to ensure that we never attempt to decrement when the current value is 1. But if n1 - k >= 1, then during the process, the values will be n1, n1-1, ..., n1 - k. The only problematic step would be if at some point we are at 1 and need to decrement further. But if the target is >=1, the smallest value encountered is the target. So if target >= 1, all steps are valid because before each decrement, the current value is at least target+1, which is >=2? Actually if target = 1, then the last step goes from 2 to 1, which is allowed because 2>1. So it's fine. So a = n1 - k is reachable if n1 - k >= 1. If n1 - k = 0, then not reachable because we can't go to 0. So a_min = max(1, n1 - k) is correct.
Thus enumeration approach is valid.
But which approach is better? The d-based approach is O(max_d) ~ 100k, while enumeration is O(k^2) ~ 40k. Both are similar. However, the enumeration might be easier to implement without needing to think about cost function. But we need to compute gcd for each pair, which is fine.
However, there is a nuance: The ranges could be up to 201 each, but for n1 large, a_min = 99900, a_max = 100100. That's 201 numbers. But we must be careful when iterating over such a large range? It's only 201 numbers, but they are large (up to 100100). That's fine.
But we need to compute gcd for each pair. 40k gcd calls per test, with numbers up to 100100, that's fine.
Alternatively, we could combine approaches: iterate over d descending, break early if found. But need to compute cost1+ cost2 quickly.
Both are fine.
However, the problem's constraints: n1, n2 up to 10^5, k up to 100. The d-based method might be more efficient if we can break early early, but it's similar.
I'll probably implement the d-based method because it's simple in logic: just compute for each d, check cost. But we need to be careful about the cost calculation and ensure we include the condition that both a,b >=1. Already handled.
But there is a subtlety: When d is large and n1 is small, cost1 = d - n1. But is it always allowed to increment to d? Yes, increments are allowed. However, we also need to ensure that the intermediate steps are valid. Increments have no restrictions. So fine.
Now, potential bug: For d <= n1, we compute min(r, d - r). But is it always safe to decrement to the floor multiple? The floor multiple is n1 - r. That value is >= d * floor(n1/d) which is at least d. Since d >=1, floor multiple >= d. But what if floor multiple = d and d = 1? Then a=1, which is reachable. If floor multiple = d and d > 1, a = d >=2? Actually if d>1, a=d, which is >=2, reachable by decrement if n1 > d. For decrement, we go from n1 down to d. The values along the way are all > d? The smallest is d, which may be 1? But d>1 then d>=2, so no problem. If d=1, floor multiple is 1, reachable. So all good.
Now, what about the ceil multiple? If we choose to go to ceil multiple = n1 + (d - r) (if r!=0) or n1 if r=0. For r!=0, ceil multiple = n1 + (d - r). That's increment, allowed. So both options are valid.
Thus cost function is valid.
Now, but we might have to consider that the floor multiple might be 0 if n1 < d? That branch goes to else. So fine.
Thus d-based method is okay.
But I'm thinking: The enumeration approach is maybe more robust because it directly simulates reachable pairs, and we can easily incorporate the total cost constraint. However, we must be careful about the ranges: a_min = max(1, n1 - k) and a_max = n1 + k. But is it always valid that all a in that range are reachable? Yes, as argued. But what about a value like a = n1 - k where n1 - k = 1? That's reachable. So fine.
But what about a = n1 - k where n1 - k = 0? Then a_min = 1, so we exclude 0. So fine.
Thus enumeration is straightforward.
Let's analyze enumeration complexity: For each test case, we have two loops. In worst case, a_range size = 2k+1 = 201, b_range = 201, product = 40401. For each pair, we compute abs diff sum and gcd. That's fine.
But we need to compute gcd. Python's math.gcd is fast.
Thus we can implement:
import math
def solve():
import sys
input = sys.stdin.readline
t = int(input().strip())
for _ in range(t):
n1 = int(input().strip())
n2 = int(input().strip())
k = int(input().strip())
a_min = max(1, n1 - k)
a_max = n1 + k
b_min = max(1, n2 - k)
b_max = n2 + k
best = 1
for a in range(a_min, a_max+1):
da = abs(a - n1)
# We could also break if da > k but it's within range so da <= k.
for b in range(b_min, b_max+1):
if da + abs(b - n2) <= k:
best = max(best, math.gcd(a, b))
print(best)
But is this sufficient? Consider that the optimal a or b might be beyond n_i + k if we use operations only on one? No, because if a > n1 + k, then da > k, sum > k. So not allowed.
Thus enumeration works.
But we must also consider that the optimal could involve a value less than 1? Not allowed.
Thus enumeration seems correct and simpler.
But wait, are we missing pairs where a is within [n1 - k, n1 + k] but b is not within [n2 - k, n2 + k]? Actually if b is not within that interval, then |b - n2| > k, sum > k, so not allowed. So we restrict to both intervals.
Thus enumeration is exhaustive.
But we must also consider that there might be pairs where a is within the interval but b is not, but sum still <= k? That cannot happen because if |b-n2| > k, then even if |a-n1| = 0, sum > k. So no.
Thus fine.
Now, potential optimization: We could precompute gcd for all pairs? Not needed.
Thus enumeration is simplest and probably correct.
But before finalizing, we should test with sample:
n1=9, n2=10, k=1.
a_min = max(1,9-1)=8, a_max=10
b_min = max(1,10-1)=9, b_max=11? Actually n2 + k = 11, so b_max=11.
Now loop:
Check pairs where sum <=1.
Compute gcds:
a=9,b=10: sum=0, gcd=1
a=9,b=11: sum=1, gcd(9,11)=1
a=10,b=9: sum=1+1=2 >1? Actually |10-9|=1, |9-10|=1, total=2 >1, not allowed.
a=10,b=10: sum=1+0=1, gcd=10 -> best=10
a=10,b=11: sum=1+1=2 >1
a=8,b=9: sum=1+1=2 >1
a=8,b=10: sum=1+0=1, gcd(8,10)=2 -> best remains 10.
a=8,b=11: sum=1+1=2
a=9,b=9: sum=0+1=1, gcd=9 -> best 10.
So best=10. Works.
Now test with n1=100000, n2=1, k=100:
a_min = max(1,100000-100)=99900, a_max=100100
b_min = max(1,1-100)=1, b_max=101
Now loops: a range size ~201, b range size ~101, product ~20301. That's fine.
We check pairs, find best gcd. The best should be 100 as we computed.
Let's simulate roughly: We need both numbers to be multiples of some d. The best d=100. For that, a can be 100000 (multiple of 100), b=100 (multiple of 100). a is within range (100000), b=100 is within 1..101. Cost: da=0, db=99, total=99 <=100. gcd=100. Could there be d=101? a=100000 not multiple, nearest multiple 101990=99990? Actually 101990=99990, cost to 99990 = 10, b to 101 cost 100, total 110 >100. Could a be 101000? out of range. So best 100. So enumeration will find 100.
Now, test with n1=1, n2=1, k=100:
a_min=1, a_max=101, b_min=1, b_max=101.
We need to find max gcd among pairs with total cost <=100. The best is (51,51) with cost 100, gcd 51. Could there be (101,1) with gcd 1? Not better. (101,101) cost 200 >100 not allowed. So enumeration should find 51.
Will enumeration consider (51,51)? a=51, b=51: da=50, db=50 sum=100 <=100, gcd=51. (52,52): sum=102 >100, not allowed. So yes.
Thus enumeration works.
Now, is there any risk of missing a pair where one number is decreased and the other increased such that the sum <= k but one of the numbers is not within its individual range? Actually individual range is defined as [max(1, n_i - k), n_i + k]. If a number is decreased, it's at least n_i - k if we stay within k steps. So it's within. If increased, it's at most n_i + k. So any reachable a satisfies that. So exhaustive.
Thus enumeration is a valid and simpler solution.
But I need to think about the scenario where n_i - k < 1. Then a_min = 1. So we consider a from 1 to n_i + k. That's correct because we can't go below 1. So enumeration covers all reachable a.
Thus we can implement enumeration.
Potential performance: For each test case, we have at most about (2k+1)*(2k+1) = up to 40401 pairs. For t=5, 202k gcd computations. Each gcd is fast. So this is absolutely fine.
We can even prune: for each a, we can compute remaining ops = k - |a-n1|, then only consider b such that |b-n2| <= remaining. But we can just loop all b and check condition; it's cheap.
Alternatively, we can precompute list of b values and their costs. But not needed.
Thus I'll implement enumeration.
But wait, there is a nuance: The operation count includes both increments and decrements. Our cost calculation uses absolute difference. However, there is also the condition for decrement: we can only decrement if >1. For a value a less than n1, we assume we can decrement to a. But what if a = 1 and n1 is large? To get from n1 to 1, we need to decrement many times. At each decrement, the condition is that the current value >1. As we decrement, when we reach 2, it's >1, then we decrement to 1, allowed. So yes, reachable. So fine.
Thus enumeration works.
But there is also condition: "Decrement n1 by 1(if n1>1)". So if n1=2 and we want to decrement to 1, it's allowed. If n1=2 and we want to decrement to 0, not allowed because after 1 we cannot decrement further. But a=0 not considered because a_min >=1.
Thus fine.
Now, also, we need to ensure that when we decrement from n1 to a, we never need to decrement when value is 1 except possibly the last step to 1? Actually if a=1, we will eventually decrement from 2 to 1, which is allowed. So fine.
Now, I'll implement enumeration.
But let's double-check with a test where n1=2, n2=2, k=1. a_min = max(1,2-1)=1, a_max=3. b similarly. Pairs with sum <=1:
Now test n1=3, n2=5, k=2. a_min=1, a_max=5; b_min=3? Actually n2=5, n2-k=3, so b_min=3, b_max=7.
Check pairs: (5,5): cost 2+0=2, gcd=5 -> ans 5. (5,6): cost 2+1=3 >2, no. (4,5): cost1=1, cost2=0 total=1 -> gcd(4,5)=1. (5,7): cost 2+2=4 >2. (6,5): cost3? a=6: cost=3 >2? Actually a=6 cost 3, so sum>2. So best 5. Also (5,5) works. So ans 5. Our earlier d-based gave 5? Actually we earlier computed for (n1=3,n2=5,k=2) we got d=5? Let's check: cost1 for d=5: n1=3<5 => cost1=2, n2=5 cost2=0 sum=2 <=2, so d=5 possible. So answer 5. So enumeration yields 5.
Now test n1=5, n2=5, k=2. a_min=3, a_max=7; b similarly. Pairs: (7,7): cost 2+2=4 >2; (6,6): cost 1+1=2, gcd=6; (5,5): 0; (4,4): 1+1=2, gcd=4; (7,5): 2+0=2, gcd(7,5)=1; etc. So max gcd=6. So answer 6. d-based: for d=6: n1=5<6 cost1=1, n2=5<6 cost2=1 sum=2 <=2, so d=6 works. d=7: cost1=2, cost2=2 sum=4 >2. So answer 6. Good.
Now test n1=1, n2=2, k=0. a_min=1, a_max=1; b_min=2? n2-k=2, max(1,2)=2, b_max=2; only (1,2): cost 0, gcd=1. So answer 1.
Now test n1=1, n2=2, k=1. a_min=1, a_max=2; b_min=1 (max(1,1)=1), b_max=3.
Pairs with sum <=1:
(1,2):0 ->1
(1,3):1 ->1
(2,2):1+0=1 -> gcd2
(2,1):1+1=2>1
(2,3):1+1=2>1
(1,1):0+1=1 ->1
So max=2. So answer 2. d-based: d=2, cost1=1, cost2=0 sum=1 <=1, so answer 2.
Thus enumeration works.
Now, note: In enumeration, we consider all a and b. But is there any scenario where the optimal gcd is achieved by a pair that does not satisfy both a and b within the intervals due to the fact that we could use operations in a way that one number goes beyond n_i + k because we can use some operations on the other? No, because total operations limited to k, so each individual distance cannot exceed k. So the intervals are exact.
Thus enumeration is exhaustive and correct.
Given that, I'll implement enumeration for simplicity.
But wait, there is one more nuance: The decrement operation is allowed only if the current value > 1. So when we consider a value a less than n1, we assume it's reachable. But is it always reachable with exactly |a-n1| operations? Yes, as argued. However, what if a = 1 and n1 = 100000? Then we need to decrement 99999 times. That's allowed because each step, the current value >1 until we get to 2, then final decrement to 1. So it's allowed. So yes.
But consider the case where n1=2 and we want a=1. That's 1 decrement, allowed. So all good.
Now, what about a value a such that n1 - a > k? Then it's outside our range, so not considered.
Thus fine.
Thus I will proceed with enumeration.
But before final code, let's test an extreme scenario: n1=1, n2=100000, k=100. a range [1,101], b range [99900,100100]. The pair (1,100000) has cost 0+0=0, gcd=1. (100,100000) cost 99+0=99, gcd(100,100000)=100. (101,100000) cost 100+0=100, gcd(101,100000)=1? 100000%101= something, gcd maybe 1. So best 100. (1,100) cost 0+99=99, gcd=1. So answer 100. Works.
Now, enumeration will loop over a in [1..101] (101 values) and b in [99900..100100] (201 values). That's about 20k pairs, fine.
Thus code:
import math
def solve():
import sys
input = sys.stdin.readline
t = int(input())
for _ in range(t):
n1 = int(input())
n2 = int(input())
k = int(input())
a_start = max(1, n1 - k)
a_end = n1 + k
b_start = max(1, n2 - k)
b_end = n2 + k
best = 1
for a in range(a_start, a_end + 1):
cost_a = abs(a - n1)
# If cost_a > k: not possible, but by construction it's <= k.
# remaining = k - cost_a
# Optionally, we could compute min/max for b based on remaining, but loop all.
for b in range(b_start, b_end + 1):
if cost_a + abs(b - n2) <= k:
g = math.gcd(a, b)
if g > best:
best = g
print(best)
But we can micro-optimize: Instead of inner loop over all b, we could precompute list of b values and their cost, then for each a, iterate over b such that cost_b <= k - cost_a. But not necessary.
We can also break early if best is already large, but not needed.
Now, check sample input.
Will this code handle the input format? The sample shows t=1, then three lines: 9,10,1. So reading as above works.
Now, test with a custom case: Input:
1
5
7
4
Our earlier expectation answer 8? Let's compute: n1=5, n2=7, k=4. a range [1,9], b range [3,11].
We need to find max gcd.
We'll simulate: (9,9): cost 4+2=6 >4; (9,8): 4+1=5 >4; (8,8): 3+1=4 => gcd=8; (10,10) out of range? a=10 >9, not allowed. So best 8. So output 8.
Our code should give 8.
Now, test with (n1=5,n2=7,k=5): a range [0? Actually a_start=max(1,5-5)=0? max(1,0)=1, a_end=10; b_start=max(1,7-5)=2? Actually 7-5=2, max(1,2)=2, b_end=12. (10,10): cost 5+3=8 >5; (10,12):5+5=10; (9,9):4+2=6; (8,8):3+1=4 => gcd8; (11,11):6+4=10; (9,6):4+1=5 => gcd3; (10,5):5+2=7; So best maybe 9? Check (9,9) cost6 >5; (10,5) cost7; (6,6):1+1=2 gcd6; (6,12):1+5=6 gcd6; (5,10):0+3=3 gcd5; (10,5):5+2=7; So 6 is max? But we earlier thought for k=5, answer 8? Wait, let's recalc for k=5: earlier we said for n1=5, n2=7, k=5 we might achieve d=8? Let's check with d=8: cost1=3 (5 to 8), cost2=1 (7 to 8) total=4 <=5 => yes, so we can achieve gcd at least 8. So there must be a pair with total ops 4 that yields gcd 8: (8,8) as we listed: cost 3+1=4. So (8,8) is within ranges: a=8 (within 1..10), b=8 (within 2..12). Yes, (8,8) is in loops, gcd=8. So best should be 8. So code will find it. Good.
Now, test with k=6: d=9? cost1=4, cost2=2 total=6 => (9,9) cost 4+2=6, gcd=9. So answer 9. So code will find (9,9) with gcd 9.
Thus enumeration works.
Now, I'll also test with n1=100000, n2=100000, k=100: a range [99900,100100], b same. The pair (100050,100050) will be considered: cost 50+50=100, gcd=100050. So best=100050. That's correct.
Now, one more test: n1=2, n2=4, k=1. a range [1,3], b range [3,5]. Check pairs: (2,4) cost 0+0=0 gcd=2; (3,4) cost1+0=1 gcd=1; (2,5) 0+1=1 gcd=1; (3,5) 1+1=2 >1; (1,4) 1+0=1 gcd=1; (1,5) 1+1=2; So best=2. Could we get gcd=3? (3,3) cost1+1=2 >1, not allowed. So answer 2. Good.
Now, consider n1=2, n2=4, k=2. a range [0? Actually a_start=max(1,2-2)=0? max(1,0)=1, a_end=4; b_start=max(1,4-2)=2, b_end=6. (3,3): cost1+1=2, gcd=3; (4,4): 2+0=2, gcd=4; (4,6): 2+2=4 >2; (2,6):0+2=2 gcd=2; So best=4. So answer 4. Could we get 5? (5,5): a=5? a range up to 4? Actually a_end=4, so a=5 not considered. Could a be 5? n1=2, k=2, max a = 4, so no. So best=4. d-based: d=4: n1=2 cost2=2, n2=4 cost0 sum=2 => ok. d=5: cost1=3, cost2=1 sum=4 >2. So answer 4.
Thus enumeration covers that.
Now, what about n1=2, n2=4, k=3. a range [1,5], b range [1? Actually n2=4, b_start=max(1,4-3)=1, b_end=7. (5,5): cost3+1=4 >3; (5,4):3+0=3 gcd=1; (4,5):2+1=3 gcd=1; (4,4):2+0=2 gcd=4; (5,7):3+3=6; (6,6):4+2=6; (5,10?) out of range. Could we get gcd=5? (5,10) b=10 not in range (max 7). (5,5) cost 3+1=4 >3. So answer still 4? But maybe (6,6) cost 4+2=6 >3. So answer 4. d-based: d=5 sum=3+1=4 >3; d=4 sum=2+0=2 <=3; So answer 4. Good.
Thus enumeration works.
Now, edge case: n1=1, n2=1, k=100. a range [1,101], b same. We will find (51,51) with gcd 51. But also (101,101) sum 200 >100 not allowed. So answer 51.
Now, what about n1=1, n2=2, k=100? a range [1,101], b range [1,102]. We can get large gcd? For d=51? Check (51,102) cost 50+100=150 >100. (51,51) cost 50+49=99 <=100, gcd=51. Could we get 68? (68,68) cost 67+66=133 >100. (68,136) cost 67+134=201. So 51 maybe max. Let's find formula: For a=b=d, cost = (d-1)+(d-2) if n2=2? Actually n1=1, n2=2. To set both to d, cost = (d-1)+(d-2) = 2d-3 <=100 => d <= 51.5 -> d=51. So answer 51. Could we do better with different multiples? For example, set a=d, b=2d: cost = (d-1)+(2d-2)=3d-3 <=100 => d<=34.33, d=34, gcd=d? Actually gcd(d,2d)=d, so same. If we set a=2d, b=d: cost=(2d-1)+(d-2)=3d-3 again. So no improvement. So 51 is max. So enumeration will find.
Thus enumeration works.
Now, about time: For each test case, we do up to ~40k gcd computations. That's fine.
But we should be careful about Python's speed if t=5 and each test case does 40k gcd. 200k gcd calls is trivial.
Thus finalize.
But is there any subtle case where the enumeration might miss a solution because a or b can be reached with exactly |a-n1| operations but not with that exact number due to decrement restrictions when a is 1 and n1 is 2? That's fine. But consider n1=2, a=1: that's one decrement. At the moment of decrement, n1=2 >1, so allowed. So fine.
Now, consider n1=2, a=0: not allowed and not in range because a_min >=1.
Thus good.
Now, I'll implement.
But wait, the problem statement says: "Decrement n1 by 1(if n1>1)". It does NOT say we cannot decrement if n1=1. So we cannot go below 1. So we are safe.
Thus enumeration method is correct.
However, I realize that the enumeration method as described does not consider the possibility of using operations to change numbers to values that are not reachable because of the >1 restriction on each decrement step. But we have argued all a in [1, n1] are reachable. However, what about a value like a = n1 - k when n1 - k = 1? That's reachable. But is it always reachable with exactly k decrements? Suppose n1 = 2, k=2. Then a_min = max(1, 0)=1, a_max = 4. So a=1 is in range. To reach a=1 from n1=2, we need 1 decrement (cost 1). That's less than k. That's okay. We are not forced to use exactly k operations. So a=1 is reachable with 1 operation. The condition is at most k operations, so we can use fewer. So it's fine.
Now, consider n1=2, a=1 is reachable with 1 operation.
Thus all a in [1, n1] are reachable with at most n1-1 operations, but if a is far from n1, it might require more than k operations, but then a would be outside the range because a_min = max(1, n1-k) ensures that if a is reachable by decrement, then n1 - a <= k. So yes.
Now, what about a value a such that a > n1? We increment; always allowed.
Thus enumeration works.
Now, finalize code.
But before final, let's think about potential optimization: we could break early if we find a gcd equal to some upper bound, but not needed.
Now, one more test: n1=10^5, n2=10^5, k=0. a range [100000,100000], b same. Only (100000,100000) gcd=100000. So answer 100000.
Now, test n1=10^5, n2=99999, k=0. a range [100000], b range [99999]. gcd(100000,99999)=1. So answer 1.
Now, test n1=10^5, n2=99999, k=1. a range [99999,100001], b range [99998,100000]. We need to check pairs with sum <=1. Options: (100000,99999) cost 0+0=1? Actually a=100000, b=99999: cost a:0, b:0? Wait b=99999 is original, cost 0; sum 0 <=1, gcd=1. (100000,100000): a=100000 cost0, b=100000 cost1, sum=1, gcd=100000. So answer 100000? Check: n1=100000, n2=99999, k=1. By incrementing n2 by 1, we get (100000,100000) gcd=100000. So answer 100000. So correct.
Thus enumeration will find (100000,100000). Good.
Now, the code is simple.
But we must be careful with input reading: There may be blank lines. Use strip.
I'll implement:
def solve():
import sys, math
data = sys.stdin.read().strip().split()
if not data: return
it = iter(data)
t = int(next(it))
out = []
for _ in range(t):
n1 = int(next(it))
n2 = int(next(it))
k = int(next(it))
a_start = max(1, n1 - k)
a_end = n1 + k
b_start = max(1, n2 - k)
b_end = n2 + k
best = 1
for a in range(a_start, a_end+1):
da = abs(a - n1)
# if da > k: continue (should not)
rem = k - da
# Optionally restrict b range further based on rem, but we can just loop and check.
for b in range(b_start, b_end+1):
if abs(b - n2) <= rem:
g = math.gcd(a, b)
if g > best:
best = g
out.append(str(best))
sys.stdout.write("\n".join(out))
But we can also precompute list of b with costs? Not needed.
This should be fine.
Now, confirm with sample:
Input:
1
9
10
1
Processing: n1=9, n2=10, k=1. a_start=8, a_end=10. b_start=9, b_end=11.
Loop:
a=8: da=1, rem=0. b from 9 to 11, only b with |b-10| <=0 => b=10. So check (8,10): gcd=2, best=2.
a=9: da=0, rem=1. b from 9 to 11: check b=9 (|1|? Actually b=9: |9-10|=1 <=1) => (9,9) gcd=9 best=9; b=10:0 => (9,10) gcd=1; b=11:1 => (9,11) gcd=1. So best=9.
a=10: da=1, rem=0 => only b=10: (10,10) gcd=10 best=10.
Thus answer 10.
Works.
Now, test with n1=5,n2=7,k=5:
a_start=0? Actually max(1,0)=1, a_end=10.
b_start=max(1,2)=2, b_end=12.
We should find best 9? Actually we earlier determined for k=5, answer 8? Wait earlier we said k=5 gives answer 8? Let's recalc: n1=5,n2=7,k=5.
We found (8,8) cost 3+1=4 => gcd 8; (9,9) cost 4+2=6 >5, so not allowed; (10,10) cost5+3=8 >5; (10,5) cost5+2=7; (5,10) 0+3=3 gcd5; (6,8) cost1+1=2 gcd2; (6,12)1+5=6; (4,8)1+1=2 gcd4; (7,7)2+0=2 gcd7; Actually (7,7) gives gcd7, cost2 <=5, so gcd7 possible. Check (7,7): a=7 (cost2), b=7 (cost0? Actually b from 7? Wait n2=7, to get b=7 cost 0? b=7 is original, so cost for a: |7-5|=2, for b:0, total=2, gcd=7. So 7 is possible. But we earlier found d=8 possible with (8,8) cost 3+1=4, so 8 >7. So answer 8. Also (9,6) cost4+1=5 gcd3. So 8 is max. For k=5, answer 8. For k=6, answer 9. So okay.
Now, enumeration will find (8,8) gcd=8, and also (9,9) not allowed, so best 8.
Thus code works.
Now, I'll produce final answer.
But we might also think about an even smarter method: Since we are doing enumeration, we can possibly break early if we find gcd equal to some upper bound, but not needed.
Now, potential issue: The ranges for a and b might be large if k is 100 and n1 is small? For n1=1, a_end = 1+100 = 101, so range size 101. For n1=100000, range size 201. So fine.
Thus code is efficient.
Now, is there any scenario where the optimal gcd is not found because the optimal a or b is exactly n_i ± k? Yes, they are included.
Thus final code as above.
Now, I'll present solution explanation: We note that the number of operations needed to change n1 to a is |a-n1|, with the constraints a ≥ 1 and for decrements, intermediate steps are valid as long as a ≥ 1. So any a with 1 ≤ a and |a-n1| ≤ k is reachable. Similarly for b. Therefore we can restrict a ∈ [max(1, n1-k), n1+k] and b ∈ [max(1, n2-k), n2+k]. Then we check all pairs (a,b) with total cost ≤ k, compute gcd, and take max. Since k ≤ 100, the loops are small.
Alternatively, we could solve by checking d from high to low using cost function, but enumeration is simpler.
I'll produce final answer.
Now, final code.
Edge Cases: Input may have extra spaces, but using split is fine.
Now, write final solution.
Also, note that math.gcd is available in Python 3.5+.
Now, produce final answer.
But I recall that the problem may be from a contest where Python may be used, and this solution is acceptable.
Thus I'll output the code.
Now, finalize.
But wait, there is a nuance: In enumeration, we consider all a from a_start to a_end inclusive. But is it possible that a = n1 - k is less than 1? Then a_start=1. But we might miss a values that are less than 1? Not needed. So fine.
Now, let's test with n1=1, n2=1, k=100. a_start=1, a_end=101. b_start=1, b_end=101. The code will loop over 101*101=10201 pairs, check each. That's fine.
Now, the inner loop checks if cost_a + cost_b <= k. That's correct.
Now, we can also slightly optimize by precomputing cost for b: For each b, cost_b = abs(b - n2). Then for each a, we can iterate over b and check if cost_b <= k - cost_a. That's what we did.
Now, I'll implement as described.
One more test: n1=100000, n2=100000, k=100. a_start=99900, a_end=100100. That's 201 values. b same. So 40401 pairs. For each compute gcd, up to 40401 gcd calls. That's okay.
But note: math.gcd for numbers up to 100100 is fast.
Now, finalize.
Potential pitfalls: Using sys.stdin.read() might be too eager for large input, but input is tiny.
Now, produce final answer.
I'll write explanation and code.
Given the problem, I'll include explanation of the reasoning.
Now, final answer. ```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))
out = []
for _ in range(t):
n1 = int(next(it))
n2 = int(next(it))
k = int(next(it))
code
# reachable ranges for the two numbers
a_start = max(1, n1 - k)
a_end = n1 + k
b_start = max(1, n2 - k)
b_end = n2 + k
best = 1
for a in range(a_start, a_end + 1):
cost_a = abs(a - n1)
rem = k - cost_a # remaining operations for b
# if rem < 0 we could skip, but by construction it never is
for b in range(b_start, b_end + 1):
if abs(b - n2) <= rem:
g = math.gcd(a, b)
if g > best:
best = g
out.append(str(best))
sys.stdout.write("\n".join(out))