We need to understand the problem: We have a point A on OX-axis with coordinate x = n (an integer). We are looking for an integer point B (also on OX-axis) such that the absolute difference between the distance from O to B and the distance from A to B is equal to k. That is, we need |OB - AB| = k, where O is at 0, A is at n, and B is an integer coordinate (could be anywhere, possibly negative? Usually OX-axis is the positive side? But coordinate can be any integer? Typically points on the axis may have integer coordinates; it says "integer point B (also on OX-axis)", likely B can be any integer coordinate, possibly negative? Since O is origin, and OX-axis includes positive and negative numbers? Usually OX-axis is the x-axis, so coordinates can be any real numbers, but B must be integer. The problem description says: "We have a point A with coordinate x = n on OX-axis. We'd like to find an integer point B (also on OX-axis), such that the absolute difference between the distance from O to B and the distance from A to B is equal to k." It doesn't specify non-negative. However, distances are absolute distances, so |B| and |n-B|? Actually distance from O to B is |B - 0| = |B|. Distance from A to B is |B - n|. So the condition is: ||B| - |B - n|| = k.
But wait, the sample: first test case n=4, k=0. They set B=2, then compute |(2 - 0) - (4 - 2)| = |2 - 2| = 0. They used (2-0) and (4-2) which are just differences of coordinates, not absolute values! Because B=2 is between 0 and 4, so distances are simply B and n-B, no absolute needed because they're positive. But if B is outside [0,n], distances would be positive but the difference might be something else. But they wrote |(2 - 0) - (4 - 2)|. That suggests they are taking coordinates: OB = B (since B is on positive axis? Actually they might assume B is on the ray from O? Typically OX-axis is oriented positive, so coordinates are non-negative? But then why specify integer point B? Possibly B can be any integer, but distances are Euclidean distances on line, which are absolute differences. So the distance from O to B is |B|, but if we restrict B to be on the positive side? In geometry problems, points on OX-axis can have negative coordinates. However, the picture (not shown) might indicate positive side. Let's examine sample 2: n=5, k=8. They increase coordinate of A by 3, so A becomes 8. Then set B as 0 or 8. For B=0: |0-0|? Actually O to B distance is 0 if B=0? B=0 gives distance O to B = 0, distance A to B = 8 => absolute difference = 8. That's valid. So B=0 is allowed. Also B=8 gives O to B=8, A to B=0 => difference 8. So both work. So B can be 0, which is O. B can also be 8 which is A. So basically they consider B integer coordinate, presumably non-negative? Because if B could be negative, there might be other solutions. But maybe they implicitly consider coordinates as positions on the line, so negative allowed but distances are absolute.
But the condition they use in explanation: "absolute difference between the distance from O to B and the distance from A to B". That is |OB - AB|. OB = |B|, AB = |B-n|. So condition: ||B| - |B-n|| = k.
But note: In the example with A=8, B=0: |0| - |0-8| = 0-8 = -8, absolute 8. So it matches. B=8: |8|-|8-8|=8-0=8. So it matches. So B can be at endpoints.
Now, if n and k are given, we can choose any integer B such that ||B| - |B-n|| = k. But we are allowed to adjust A by increments/decrements of 1 (each step changes n by +/-1) to make it possible. We want minimum steps.
We need to find, given initial n, k, minimal d such that there exists integer m (the new coordinate of A after moving) and integer B with ||B| - |B-m|| = k. Actually we can change A, and then choose B accordingly. So we need to find minimal number of steps to adjust n to some integer n' such that the equation has an integer solution B. Then answer is |n' - n| (since each step changes by 1). So we need to find the minimal distance (in integer steps) from n to some m for which condition holds.
So the problem reduces to: For given n and k (0 ≤ n,k ≤ 10^6), find the minimal non-negative integer s such that there exists integer m with |m - n| = s (actually steps: we can increase or decrease, so absolute difference) and there exists integer B satisfying ||B| - |B - m|| = k.
We can also think: we can directly choose to move A to some m (maybe same as n) and then choose B. So we need to determine for which m does there exist integer B such that ||B| - |B-m|| = k.
We need to characterize the set of m for which the equation has an integer solution B. Then compute the distance from n to the nearest such m.
Given m (non-negative? Actually m is coordinate of A after moving; initial n is non-negative, but we can increase or decrease, so m could be any integer? The problem says "increase or decrease the coordinate of A by 1". Initially n ≥0 (given constraints 0 ≤ n ≤ 10^6). But after moving, m could become negative? Possibly yes, because we can decrease from 0. But the problem might assume points are on OX-axis, which usually includes positive coordinates? However, typical geometry: OX-axis is the whole line, but points could have negative coordinates. But maybe they consider non-negative coordinates? The input constraints have n ≥0, k≥0. They didn't say m must be non-negative, but after moving we might have m negative. Is that allowed? Usually coordinate of a point can be any integer? It might be allowed. But let's see if negative m would affect condition? The condition uses |B - m|, and |B|. B is integer. So negative m is possible. However, the minimal steps might never require going negative if we can achieve solution with non-negative m? Possibly for some cases negative might be beneficial? Let's think.
We can attempt to characterize the condition. Let m be any integer (could be negative). For any integer B, define f(B,m) = ||B| - |B - m||. We want f(B,m) = k.
We need to know for which m there exists B integer such that f = k.
Observation: The expression ||B| - |B-m|| can be thought of as the distance difference from B to two points O and A (where A at m). There is known property: For points on a line, the set of points where the difference of distances to two fixed points is constant is a hyperbola. But here we require absolute difference, so it's like two branches. Also B integer.
But we can simplify by considering the possible values of ||B| - |B-m||. Since absolute values, we can remove some by cases based on signs of B and B-m.
Let's define B relative to 0 and m.
We consider the positions of B on the number line relative to 0 and m. There are several intervals: (-∞, min(0,m)], [min(0,m), max(0,m)], [max(0,m), ∞). But careful with endpoints.
Alternatively, we can think: For any real B, the value of |B| - |B-m| can be expressed piecewise linear. The absolute of that gives k.
Better: There is known result: For two fixed points on line, the absolute difference of distances to a variable point is at most the distance between the two fixed points. Specifically, by triangle inequality, | |B-0| - |B-m| | ≤ |0-m| = |m|. And equality can be achieved when B is on the ray starting from one point away from the other, i.e., B ≤ min(0,m) or B ≥ max(0,m). Actually, let's analyze.
Let d = |m| (distance from O to A). For any point B, the difference | |B| - |B-m| | is at most d, and can achieve any integer value from 0 to d? But not exactly any integer: For integer B, maybe some constraints. But we need to see if there exists B integer such that the absolute difference equals k.
But note: The maximum possible absolute difference is d, achieved when B is far to one side (e.g., B large positive if m>0, then |B| - |B-m| approximates m, but actually if B >= max(0,m), then |B| = B, |B-m| = B-m, so difference = m. If m>0, then m is positive, absolute = m. If B <= min(0,m) (say m>0, min is 0), then B <=0 => |B| = -B, |B-m| = -(B-m) = m-B? Actually if B <=0 and m>0, then B <=0 < m, so B-m negative, so |B-m| = -(B-m) = m-B. Then |B| - |B-m| = -B - (m-B) = -m. Absolute = m. So maximum is m = d. Similarly if m<0, then d = |m|, maximum is |m|.
So the condition requires k ≤ |m|, because otherwise impossible. Also note k is non-negative.
But also, can we achieve any integer k between 0 and |m|? Possibly yes, by choosing B appropriately. But need to consider parity? Because distances are integer? B integer, so |B| and |B-m| are integers, so their difference is integer. So k must be integer; that's fine. But are all integers from 0 to |m| achievable? Let's test with some m.
Take m=5. d=5. We want k=0..5. For k=0, choose B=2.5? But B integer: B=2 gives |2|-|2-5|=2-3= -1, absolute 1. Not 0. B=3 gives 3-2=1. B=1 gives 1-4=3. B=0 gives 0-5=5. B=5 gives 5-0=5. B=... Actually need absolute difference 0: that would require |B| = |B-5|. For real numbers, the solution is B=2.5, not integer. So for m=5, k=0 may not be achievable with integer B. Indeed the sample 2: n=5, k=8, they increased A to 8, making m=8, because k=8 > m=5, so impossible. They increased to 8, so m=8, k=8 achievable. For m=8, k=8 achievable by B=0 or 8. But what about m=5, k=5? Achievable: B=0 gives 5, B=5 gives 5. So k=5 is achievable. k=4? For m=5, can we get 4? Let's try B=1: |1|-|1-5| = 1-4= -4 => 4. So yes. k=3? B=2: 2-3= -1 => 1? Actually 2-3 = -1 => abs=1. B=3: 3-2=1 =>1. B=-1: |-1|-|-1-5| =1-6= -5 =>5. B=4: 4-1=3 =>3? Actually 4-1=3 => absolute 3. So yes, B=4 gives 3. So m=5 gives k=3 achievable. k=2? B=?? B=1 gives 4, B=2 gives 1, B=3 gives 1, B=4 gives 3, B=0 gives5, B=5 gives5. B=6? 6-1=5, B= -2: 2-7= -5 =>5. So maybe 2 not achievable? Let's systematically: For m>0, we can derive expression for f(B) = ||B| - |B-m||. Consider B in different regions.
Case 1: B >= m (>=0 since m>0, and B>=m>0). Then |B|=B, |B-m| = B-m, so f = |B - (B-m)| = |m| = m. So any B >= m gives f = m. So only gives k = m.
Case 2: B <= 0 (since 0 ≤ m, B <=0). Then |B| = -B, |B-m| = -(B-m) = m - B (since B-m ≤ 0? Actually if B <=0, then B-m <= -m <0, so absolute = -(B-m)= m-B). So f = | -B - (m - B) | = | -m | = m. So also only m.
Case 3: 0 ≤ B ≤ m. Then |B| = B, |B-m| = m-B (since B ≤ m, B-m ≤0). So f = |B - (m-B)| = |2B - m|. So f = |2B - m|. For B integer between 0 and m inclusive, f can take values: when B=0: f = m; B=m: f = m; for B from 0 to m, 2B-m ranges from -m to m, stepping by 2. So f takes values of the form |m - 2B|. That's m, m-2, m-4, ..., down to 0 or 1 depending on parity of m. Specifically, if m is even, we can achieve 0 when B = m/2; if m is odd, the minimum absolute is 1 when B = (m±1)/2. So in the region [0,m], f can achieve all values of same parity as m (i.e., m mod 2) from 1 (or 0) up to m, stepping by 2. Actually m mod 2: if m even, f can be even numbers from 0 to m; if m odd, f can be odd numbers from 1 to m.
Now what about B in negative but >0? Already covered B <=0 yields f=m only. B > m yields f=m only. So overall possible k for given m (positive) are: k = m (always possible), and also any integer k such that 0 ≤ k ≤ m and k ≡ m (mod 2)? Wait, from region [0,m] we get values with same parity as m? Let's check m=5 (odd). From region we got f=|2B-5|, B integer 0..5. The possible values: B=0:5; 1:3; 2:1; 3:1; 4:3; 5:5. So we got 5,3,1. So odd numbers from 1 to5. Indeed parity matches m (odd). So we get k odd ≤ m, and also k=m (already included). So overall possible k: all odd numbers from 1 to 5 inclusive, and also k=5 (already). No even numbers except possibly if m even? For m=4 (even). Let's test: m=4, region [0,4]: B=0:4;1:|2-4|=2;2:|4-4|=0;3:|6-4|=2;4:4. So possible: 4,2,0. So even numbers from 0 to4. Also endpoints B>=4 give 4, B<=0 give 4. So possible k: 0,2,4. So again, k values that have same parity as m, and also k=m (which is of that parity). So it seems that for m>0, the attainable k are exactly those integers k where 0 ≤ k ≤ m and k ≡ m (mod 2). Additionally, is k=0 always possible only when m even? Yes, when m even and B=m/2 gives 0. So condition: m >= k and (m - k) is even.
But is there any possibility for other k? For m=0? Then A at O. Then distance from O to B is |B|, distance from A to B also |B|, so f = ||B| - |B|| = 0. So only k=0 possible. So condition: if m=0, only k=0.
What if m negative? Let m < 0. Then we can apply symmetry. Because f(B,m) = ||B| - |B-m||. Note that replacing m by -m and B by -B? Let's check: |B| - |B-m| vs |(-B)| - |(-B) - (-m)|? Actually if we reflect the line: Let B' = -B, m' = -m >0. Then |B| = | -B'| = |B'|, and |B-m| = | -B' - (-m')? Wait: m is negative, let m = -M with M>0. Then A is at -M. Then condition is ||B| - |B+ M||? Since B - (-M) = B+M. So f = ||B| - |B+M||. This is symmetric to the case with m'=M but with B possibly shifted? Actually if we define C = -B, then |B| = |C|, and |B+M| = | -C + M| = |M - C|. So f = ||C| - |M - C|| = same as before with point at M. So it reduces to the case with positive m. So the condition for m negative is same: |m| = M must satisfy M >= k and (M - k) even? But careful: For m negative, the condition from above analysis: with m = -M, we have f(B) = ||B| - |B+M||. By analysis, the possible k values are those with M >= k and (M - k) even? Let's test with m=-5 (M=5). Could we get k=3? Choose B? For m=-5, we need ||B| - |B+5|| = 3. Try B = ? If we choose B = -2? Then |B|=2, |B+5|=3, difference =1. B=-3: |3|-|2|=1. B=-4:4-1=3, yes! So k=3 works. For k=2? Should not work because M=5 odd, parity says only odd k. Check B=-1:1-4=3? Actually B=-1: |1|-|4| = 1-4= -3 =>3. B=0:0-5=5. B=1:1-6=5. B=2:2-7=5. B=3:3-8=5. B=-2 gave 1. So indeed 2 not possible. So condition seems: For any integer m (including negative), define d = |m|. Then there exists integer B with f(B,m)=k iff k ≤ d and (d - k) is even. Additionally, for m=0, d=0, condition becomes k=0 and (0-0 even) holds. So that matches.
But wait, check edge case: m=1, d=1, parity condition: d - k even => 1 - k even => k odd? Since 1 odd, k must be odd? But possible k: from earlier, m=1 gives B in [0,1]: B=0: f=1; B=1:1; B=? also region B<=0 gives 1, B>=1 gives 1. So only k=1. So condition holds: k=1 ≤1 and (1-1)=0 even. So good.
m=2: d=2, condition: k ≤2 and (2-k) even => k even. So possible k:0,2. Indeed earlier we saw 0 and 2 possible. Good.
So condition: For given m, possible k iff |m| >= k and (|m| - k) % 2 == 0.
Now, does this cover all possibilities? What about k=0 and m odd? Condition says m odd => (odd - 0) odd, so not allowed. Indeed we saw m=5, cannot get 0. So consistent.
Thus, existence of integer B for a given A coordinate m is exactly that: d = |m| must satisfy d >= k and (d - k) even.
But wait, is there any possibility when k = 0 and m = 0? d=0, (0-0) even true. So okay.
Now, also consider the case where m is negative? d = |m| same condition.
Thus, our problem reduces to: Given initial n (>=0), we can change it to some integer m (any integer, positive or negative) at cost |m - n|. We want to find m such that |m| >= k and (|m| - k) is even. Minimize |m - n|.
Because we can increase or decrease by 1 per step, the minimal number of steps is the minimal distance from n to any m satisfying the condition.
But note: We are allowed to move A step by step; we can only change n to another integer (could be negative). So yes.
So we need to compute:
Let S = { m ∈ ℤ : |m| >= k and (|m| - k) % 2 == 0 }.
Find min_{m ∈ S} |m - n|.
That's the answer.
Since n and k up to 1e6, we can compute directly by checking possibilities near n.
But we need to handle t up to 6000, so O(1) per test case preferably.
We can reason analytically.
We need to find integer m minimizing distance to n such that d = |m| satisfies d >= k and d ≡ k (mod 2) (since (d - k) even => d ≡ k mod 2).
Thus, condition on |m|: it must be at least k, and parity matches k (mod 2). That's all.
So essentially, we need to find integer d (nonnegative) such that d >= k and d ≡ k (mod 2). And then we can choose m = d or m = -d (if we allow negative). Since we can choose sign, that gives two possible m values for a given d: m = d or m = -d (unless d=0 then both same). The distance from n to m is min(|n - d|, |n + d|) if negative allowed? Actually m = -d yields distance |n + d| (since n is non-negative). So we can consider both positive and negative m.
But is there any advantage to choosing negative m over positive? For a given d, the m = d is at coordinate d (>=0). m = -d is at -d (<=0). Since n >=0, the distance to d is |n-d|, to -d is n+d. Clearly n+d >= n >=0, and typically larger than |n-d| unless n is small? Actually |n-d| ≤ n+d, with equality only when one of them zero? Actually triangle inequality: n+d = |n - (-d)| >= |n - d|? Not always: if d >= n, then |n-d| = d-n, and n+d = n+d, which is greater than d-n if n>0. If d < n, then |n-d| = n-d, and n+d > n-d for positive d. So distance to negative m is always larger than or equal to distance to positive m? Compare |n - d| vs n+d. Since n+d >= n - d? Actually n+d - (n-d) = 2d >=0, so n+d >= n-d. But |n-d| could be less than n-d if n-d negative? Wait, careful: n and d nonnegative. Then |n-d| = absolute difference. We have two cases:
- If d ≤ n: then |n-d| = n-d. Compare n+d vs n-d: obviously n+d > n-d (unless d=0). So positive m (d) gives distance n-d, negative m gives distance n+d which is larger.
- If d ≥ n: then |n-d| = d-n. Compare n+d vs d-n: n+d - (d-n) = 2n ≥0. So positive m gives distance d-n, negative gives n+d > d-n if n>0. So positive m always gives smaller or equal distance (equal only when n=0? If n=0, then positive m: |0-d| = d, negative m: |0 - (-d)| = d, same). So the minimal distance for a given d is achieved by taking m = d (i.e., positive) if n >=0. So we can restrict to m >=0 (non-negative) without loss of optimality, because negative gives larger or equal distance. However, is it possible that the positive m does not satisfy parity condition? Actually m = d, so |m| = d, same parity condition. So if d satisfies condition, m=d is a valid candidate. So we can ignore negative m. But careful: could there be a scenario where no non-negative m satisfies the condition, but some negative m does? The condition only depends on |m|, so if there exists any m (including negative) satisfying, then there also exists a non-negative m with same |m|, namely m = |m|, which also satisfies condition (since |m| unchanged). So the set S contains all non-negative d that meet the conditions, as m = d. Therefore we can restrict to m >= 0. So we only need to consider m >=0. Good.
Thus the problem reduces to: Given n (>=0) and k (>=0), find the minimal non-negative integer Δ = |n - m| where m is an integer >=0 such that m >= k and (m - k) is even. That's it.
Now we can compute the minimal distance.
We need to find an integer m >=0 satisfying:
- m >= k
- m ≡ k (mod 2) i.e., m % 2 == k % 2
We want to minimize |n - m|.
Essentially, we need to find the integer m of a given parity (same parity as k) that is at least k, and as close as possible to n.
We can consider two cases: If n itself satisfies n >= k and n % 2 == k % 2, then we can take m = n, distance 0.
Otherwise, we need to adjust n to some m. But note we can only change n to m (by moving A). But we are allowed to move A to any m, not necessarily by altering n? Actually we start at n, we can increase or decrease. So we can move to any integer m (maybe less than n). So we are not restricted to m >= n? We can decrease too. So m can be less than n, as long as m >= k (since condition requires m >= k). So we can consider m < n, but must be >= k. So m is in [k, ∞). So we need the closest integer to n in the set T = { m ∈ Z, m >= k, m ≡ k (mod 2) }.
Because if n < k, then the minimal m that satisfies m>=k is at least k, so we need to increase to at least k with correct parity.
If n >= k, then we can try to find m close to n in T.
But note: T includes numbers that are >=k and have parity equal to k. So essentially, T = { m = k + 2t, where t >= 0 integer }.
Because starting from k, adding even numbers preserves parity. So T = { k, k+2, k+4, ... }.
Thus we need to find the integer of the form k + 2t that is closest to n (in absolute difference), subject to being >= k (automatically). However, if n is less than k, the closest is obviously k (since all are >= k, and k is the smallest). So the minimal distance is k - n (since n < k). But wait, if n < k, distance to k is k - n. Could there be a number of form k+2t that is less than k? No, because t>=0 => m>=k. So indeed k is the closest. However, what about n > k? Then we can also consider m less than n, possibly exactly n if n has same parity as k; if not, the nearest could be n-1 or n+1? But they must have correct parity. But also m must be >=k, so if we go lower than n, we can only go down to at least k. So we need to find the integer m in the arithmetic progression that minimizes |n - m|.
So the answer is:
If n >= k:
If (n - k) % 2 == 0, then answer = 0 (since m = n works).
Else, we need to find the closest numbers: one less than n (n-1) and one greater than n (n+1). But they must be >= k. Since n >= k, n-1 >= k-1; it might be less than k if n = k? Let's examine.
Case n = k: If n = k, then n >= k. (n - k)%2 = 0, so answer 0? Check condition: m=n=k, need m >= k ok, parity: (k - k) even true, so works. So answer 0. Indeed sample: n=1000000, k=1000000 gives output 0. That matches.
If n > k and parity mismatched, we consider two candidates:
m1 = largest number <= n that is in T. Since T is arithmetic progression with difference 2, the numbers less than or equal to n are those with m = k + 2t ≤ n. The largest such is: let t_max = floor((n - k)/2). Then m1 = k + 2t_max. Since n - k is odd? Actually if n - k is odd, then floor((n-k)/2) is integer, m1 = k + 2floor((n-k)/2). This m1 has same parity as k and is ≤ n. Its distance to n is n - m1 = (n - k) - 2*floor((n-k)/2). That is either 0 if (n-k) even, or 1 if (n-k) odd. Since we are in mismatched case, (n-k) is odd, so distance = 1.
The next candidate m2 = m1 + 2 = k + 2*(floor((n-k)/2)+1) = m1+2. This is > n. Its distance = m2 - n = (m1+2) - n = 2 - (n - m1). Since n - m1 = 1, distance = 1. So both distances are 1? Let's compute: if n - k is odd, then n - m1 = 1, and m2 = m1+2, so m2 - n = 2 - 1 = 1. So both are at distance 1. However, is m2 always >= k? Yes, since m1 >= k, m2 > m1 >= k. So both are valid. So answer = 1. But need to ensure m1 is indeed >= k? Since n > k, n - k >=1, floor((n-k)/2) >=0, so m1 >= k. So ok.
But wait, what if n - k is odd and n = k+1? Then m1 = k (since floor((1)/2)=0) => m1=k, distance = 1. m2 = k+2, distance = 1 (since (k+2)-(k+1)=1). So answer 1. Sample: n=1, k=0? Actually n=1, k=0: n >= k, (n - k) = 1 odd, so answer 1. Sample input: 1 0 output 1. That matches. Good.
Now consider n < k: then we need to increase to at least k. The smallest m in T is k. So distance = k - n. But we must also check parity? Actually k itself is in T because it has parity k mod 2 (trivially). So m = k works. So answer = k - n. Example: n=0, k=1000000 output 1000000 (sample). n=0, k=0 output 0. Good.
Now what about n=4, k=0: n >= k, (4-0) even, so answer 0. Sample: 0.
n=5, k=8: n < k, so answer = 8-5 = 3. Sample: 3. Yes.
All sample test cases match.
Thus solution appears to be:
If n >= k:
if (n - k) % 2 == 0:
answer = 0
else:
answer = 1
Else:
answer = k - n
But wait, need to consider the possibility that n is negative? No, initial n is 0 ≤ n ≤ 10^6, so non-negative.
Check more edge cases: n=0, k=1 => n<k, answer = 1-0=1. But is there a solution? Need m>=1 and m ≡1 mod2. The smallest m=1. Distance from 0 to 1 is 1. But can we do with m= -1? That would be distance 1 as well, but m negative would also satisfy? m=-1 gives |m|=1 >=1, parity: 1-1=0 even, condition holds, distance from 0 to -1 is 1. So also 1. So answer 1. Does there exist any m with less distance? m=0? But m=0 gives |0|=0 <1, fails. So answer 1. So seems fine.
Now test n=2, k=3: n<k => answer = 1. Check: need m>=3 and m odd (since k=3 odd). m=3 gives distance 1. Could there be m=2? No because 2<3. So correct.
Test n=3, k=2: n>=k, (3-2)=1 odd => answer 1. Check: m must be >=2 and parity even (since k even). m=2 works? 2 >=2, even, but distance from 3 to 2 is 1. Also m=4 distance 1. So answer 1. But could there be m=3? 3 parity odd, fails. So yes.
Test n=3, k=4: n<k => answer=1. m=4 works.
Test n=3, k=5: answer=2.
Test n=3, k=0: n>=k, (3-0)=3 odd => answer 1? But wait, condition for k=0: need m>=0 and m parity even (since k=0 even). So m must be even. n=3 is odd, so closest even m >=0 are 2 and 4. Distance 1 to 2, or 1 to 4? Actually |3-2|=1, |3-4|=1. So answer 1. But check sample: n=1,k=0 gave answer 1, consistent. So this matches.
But hold on: For k=0, condition is m>=0 and (m-0) even => m even. So indeed n=3 -> answer 1. But is there any possibility with m=3 itself? m=3 gives |3|-k? Condition: m>=0 and (m - 0) even? 3 odd so fails. So yes.
Now consider n=0,k=0: answer 0.
Now, what about n=10, k=3: n>=k, (10-3)=7 odd -> answer 1. Check: need m odd, >=3. 10 is even, so closest odd are 9 and 11. Distances 1 and 1. So 1. Works.
Now, is there any scenario where answer could be more than 1 when n>=k and parity mismatch? According to above, the nearest numbers in T are at most distance 1 away because T is spaced by 2, and n is between two consecutive elements of T (since parity mismatch, n is not in T, so it lies strictly between two elements: one at m1 = largest ≤ n, which is at distance ≤1 (since the gap to next is 2) but could be distance 0 if n is in T; but if not, the distance from n to the nearest T is at most 1. However, is it always true that if n >= k, there is always a T element within distance 1? Consider n exactly equal to k-1? But n >= k case implies n >= k, so n cannot be k-1. So n >= k. The progression T starts at k. So the set of T contains numbers: k, k+2, k+4,... So if n >= k, then the distance from n to the nearest element of T is either 0 (if n and k same parity) or 1 (if opposite parity). Because the spacing is 2, and n is at least k, so the distance to the nearest integer in that arithmetic progression is at most 1. Formal: Let d = n - k. Write d = 2q + r, where r = 0 or 1. If r=0, n in T. If r=1, then n is between k+2q and k+2q+2, both in T, distances 1 each. So indeed answer is either 0 or 1.
But wait: What about the case where k > n? Then answer is k - n. Could that be larger than 1? Yes.
Now check if there any additional constraints: m must be integer, but also B must exist. Our derived condition is necessary and sufficient? Let's double-check thoroughly.
We derived condition based on analysis of f(B,m). But we must ensure that for m satisfying condition, there is indeed an integer B. For m>0, we gave explicit B in region [0,m] to achieve any k with same parity: choose B = (m ± k)/2? Actually from f = |2B - m|, we set |2B - m| = k. Since k ≤ m and same parity, (m - k) even, then (m ± k)/2 are integers. If we choose B = (m - k)/2 (if within [0,m]) yields 2B - m = -k, absolute k. And (m - k)/2 is between 0 and m because k ≤ m. So that's a valid integer B. Similarly B = (m + k)/2 ≤ m? Actually (m + k)/2 ≤ m only if k ≤ m, it's ≤ m, but also >= m? Actually (m + k)/2 >= m/2, and if k ≤ m, then (m+k)/2 ≤ m? Check: (m+k)/2 ≤ m => m+k ≤ 2m => k ≤ m, true. So also within [0,m]. So both are valid. So indeed for m>0 and condition holds, integer B exists.
For m=0, only k=0, and any B works? Actually condition: f=||B| - |B|| = 0, so yes.
For m negative, similar.
Thus condition is correct.
Now what about m = 0, k=0, B any integer works, so condition holds.
So solution is as simple as above.
But let's verify with some random tests to be sure.
We could brute for small n,k to see if any case where answer not as per this formula.
Let's manually test n=2, k=1: n>=k, (2-1)=1 odd -> answer 1. Check: m must be >=1 and odd. So m=1 or 3. Distance from 2 to 1 =1, to 3 =1. So answer 1. But is there any B for m=1? m=1, need k=1. B? For m=1, we can use B=0 gives f=1? 0: |0|-|0-1|=0-1=1, ok. So works. So steps=1. Could we get m=2? m=2 fails parity. So indeed need at least 1 step.
What about n=2, k=2: n>=k, (2-2)=0 even -> answer 0. m=2 works? need m>=2 and even. So m=2. B? For m=2, need k=2. B? B=0 gives 2, or B=2 gives 2. So works. So 0 steps.
n=2, k=3: n<k -> answer 1 (3-2=1). m=3 works? m=3 odd, k=3, B? B=0 gives 3, works. So 1 step.
n=2, k=4: n<k -> answer 2. m=4 even, works? B=0 gives 4, yes. So 2 steps.
n=2, k=5: answer 3.
n=3, k=5: answer 2. m=5 works? m=5 odd, k=5. B=0 gives 5, works. Steps 2.
n=3, k=6: n<k -> answer 3. m=6 even, works? B=0 gives 6, works. Steps 3.
n=3, k=7: answer 4.
Now consider n=0, k=1: answer 1. m=1 works? yes.
n=0, k=2: answer 2. m=2 works? B? For m=2, need k=2. B=0 gives 2, works.
n=0, k=3: answer 3.
Now consider n=5, k=5: answer 0.
n=5, k=4: n>=k, (5-4)=1 odd -> answer 1. m=4 works? m=4 even, k=4. B? For m=4, need B? B=0 gives 4, works. So 1 step (decrease by 1). Or increase to 6? 6 even, distance 1 also. So 1.
n=5, k=6: n<k -> answer 1. m=6 works? m=6 even, k=6, B=0 gives 6, works. So increase by 1.
n=5, k=7: answer 2. m=7 odd? k=7 odd, m=7 works? Yes. So increase by 2.
n=5, k=8: answer 3 (as sample).
Now consider n=1, k=1: answer 0.
n=1, k=2: n<k -> answer 1. m=2 works? m=2 even, k=2, B=0 gives 2? Actually B=0 => |0|-|0-2|=0-2=2, works. So 1 step.
n=1, k=3: answer 2. m=3 works.
Seems consistent.
Now we should also think about possibility of moving A to negative values could yield smaller steps? For n>=0, we argued that for any d = |m|, the positive m gives smaller distance. However, suppose n is very small, say n=0, k large. The answer we computed is k - n = k. But if we consider negative m, we also need d = |m| >= k. The smallest |m| is k. So m could be -k, distance from 0 to -k is k as well. So same.
But could there be a scenario where n is large, k is even smaller, but parity mismatch, and moving to a negative m could give distance < 1? For n>=k, if parity mismatch, we said answer = 1. Could negative m give answer 0? That would require m such that |m - n| = 0, i.e., m = n. But n is non-negative. So m = n is non-negative. So if n doesn't satisfy condition, then no zero with positive m. But negative m? m = n is non-negative, so no. So answer cannot be less than 1.
What about distance less than 1? Not integer, so no.
But maybe there is a scenario where n >= k but the closest m in T is not within 1 because the progression starts at k, but if n is just slightly above k but the next T after k is k+2, and n might be k+1, distance to k is 1, to k+2 is 1. So that's 1. If n = k+0.5? but n integer, so n = k+1 gives distance 1. So indeed distance 1.
Thus answer is as simple as described.
However, there might be nuance: The condition derived requires m >= k. But is it possible to achieve the desired k with m < k? Let's double-check: If |m| < k, is it possible to have ||B| - |B-m|| = k? As argued, maximum possible absolute difference is |m|, so if |m| < k, it's impossible. So m must satisfy |m| >= k. So yes, m must be at least k in absolute value. Since we consider non-negative m, m >= k.
Thus solution is trivial.
But wait, in sample 2: n=5, k=8. n=5 < 8, so answer 3 = 8-5. That matches.
Now we should also consider scenario where k is 0. Then condition is m >= 0 and m even? Actually (m - 0) even => m even. So m must be even. For n even, answer 0; for n odd, answer 1 (since distance to nearest even). That matches sample: n=1,k=0 => answer 1; n=0,k=0 => 0; n=4,k=0 => 0.
Now double-check the note: For test case 2 picture (not provided) but they increased A by 3 to get 8. That's exactly 8-5=3.
Thus answer is straightforward.
But wait, are there any hidden intricacies? Perhaps the problem originally considered B also on OX-axis but maybe B can be any integer, including negative? That doesn't affect condition as we already accounted.
Another nuance: Could B be chosen such that the distance difference equals k, but maybe B is not required to be integer? It says integer point B. So B integer. We already accounted.
Thus the solution is:
For each test case:
If n >= k:
if (n - k) % 2 == 0: print(0)
else: print(1)
else:
print(k - n)
But wait, need to consider case where k = 0 and n = 0? (0-0)%2 =0 => 0.
Now, think about k > 0 and n = 0. Then n < k => answer = k. That seems fine.
Now, but is there any scenario where n is very large, k is very small, and (n - k) even => 0? Yes.
Now, could there be a scenario where n is less than k but (k - n) is odd and we might get a smaller distance by moving to some m > k? For n < k, the smallest m we can achieve is k, distance = k - n. But could there be an m < k that still satisfies condition? No, because condition requires m >= k. So we cannot go below k. So minimal distance is at least k - n. Could we go to m > k to possibly get a smaller distance? That would be farther because distance = m - n, and since m >= k, the minimum is at m = k. So answer is k - n.
But consider n=0, k=2: answer 2. But maybe m= -2? That's distance 2 as well. So same.
Thus answer is correct.
Now, let's check constraints: n,k up to 1e6, t up to 6000, trivial.
But wait, is there any case where k is larger than 0 and we need to consider that m must be integer but also B integer? Already covered.
Thus solution is O(t).
But we must be cautious: The condition derived was m >= k and (m - k) even, assuming m non-negative. But what if m negative? Already argued not better.
Thus answer stands.
However, let's consider extreme cases: n=10^6, k=0 => if n even? 10^6 is even, so answer 0. If n odd? Then answer 1. That's trivial.
Now, test case: n=999999, k=1 => n >= k, (999999-1)=999998 even, so answer 0. Indeed m = n works? n odd, k=1 odd, condition: m >=1 and (m - k) even: (999999-1)=999998 even => yes. So m=999999 works. Check existence of B: need B such that ||B| - |B-999999|| = 1. For m odd, we can take B = (m - 1)/2 = 499999, gives difference 1. So works. So answer 0.
Thus all good.
Now we could also reason directly: The answer is:
if n < k: ans = k - n
else: ans = ( (n - k) & 1 ) // 0 if same parity else 1
But is there any scenario where ans could be more than 1? Let's think about n > k but maybe n is such that the nearest m satisfying m >= k and parity condition is not within 1? As argued, the arithmetic progression from k with step 2 covers all numbers with parity matching k, and since n >= k, the distance from n to the nearest such number is at most 1 (since step 2). However, is it guaranteed that there is always a number in T less than or equal to n? Yes, because if n >= k, then either n itself has same parity as k (then it's in T) or if not, then n-1 has same parity as k? Actually if n parity differs from k, then n-1 has same parity as k? Because flipping parity. But we must check if n-1 >= k. Could it be that n-1 < k? This can happen only if n = k and parity differs? But if n = k, then n and k have same parity trivially? Wait, parity of n and k: if n = k, then n - k = 0 even, so parity matches. So for n >= k and parity mismatch, we must have n > k. So n-1 >= k? If n > k, then n-1 >= k? Not necessarily: if n = k+1, then n-1 = k, which is >= k. So yes, n-1 >= k. So m = n-1 is in T? Need to check parity: since n and k have opposite parity, and n = k+1, then k+1 parity opposite to k, so k is same parity as k obviously, so m = k works. So distance =1. So indeed always at least one of n-1 or n+1 is in T and distance 1. So answer never exceeds 1 when n >= k.
Thus solution is correct.
Now, we should provide reasoning in final answer.
But wait, we must also consider that we might be allowed to move A multiple steps, but the answer is the minimum number of steps. So it's fine.
Edge Cases:
- t up to 6000, we just compute per case.
Double-check sample input:
6
4 0 -> n>=k, (4-0)=4 even ->0
5 8 -> n<k -> 3
0 1000000 -> n<k -> 1000000
0 0 -> n>=k? 0>=0 true, (0-0)=0 even ->0
1 0 -> n>=k, (1-0)=1 odd ->1
1000000 1000000 -> n>=k, (1000000-1000000)=0 even ->0
Matches output.
Thus solution is trivial.
But before finalizing, let's think if there is any trick: The condition we derived might be missing the possibility where m is negative but leads to a smaller distance than the formula because n is positive but maybe moving to a negative m with |m| = something could give distance less than k-n? For n < k, we computed answer = k - n. But what if we move to a negative m = -k? Distance = |n - (-k)| = n + k. That's larger than k - n (since n>=0, k>=n, so n+k >= k-n? Actually k - n <= k + n? Since n≥0, k-n ≤ k+n. So positive m is better or equal. So no.
But what if we move to m = -(k+2) etc? Even larger.
So no.
Now consider n > k but parity mismatch, we said answer=1. Could moving to a negative m give distance 0? Only if m = n, but n is positive, so m negative not equal. Could moving to negative m give distance less than 1? Not integer. So no.
Thus the formula is final.
Now, let's ensure we have considered the case where k=0 and n=0: answer 0.
So solution is:
For each test case:
if n >= k:
if (n - k) % 2 == 0: print(0)
else: print(1)
else:
print(k - n)
But wait, what about the case where k is large (e.g., 10^6) and n is also large? Works.
Now, is there any scenario where k > 0 and n = k-1, then n < k, answer = 1. That seems correct.
Now, could there be scenario where k is odd, n is even and n = k+1? Then n >= k, (n - k)=1 odd, answer 1. That matches.
Thus done.
But I recall a Codeforces problem: "1367A? No" Actually it might be from Codeforces Round #1430 (Div.2) or something: "Short Substrings"? Not that. There is a problem "A. Find a Number" maybe? Actually this problem looks like Codeforces Round #1400: "A. Point on Spiral"? Not sure.
Anyway, it's trivial.
But before concluding, we should double-check the derivation thoroughly, because sometimes such problems have nuance: The absolute difference between distance from O to B and distance from A to B is k. That is | |B| - |B - n| | = k. We considered m (the new coordinate of A). The condition for existence of integer B is indeed that |m| >= k and (|m| - k) even? But we must ensure that there is an integer B achieving it. We gave construction: For m>0, pick B = (m - k)/2 (if that is integer, i.e., m-k even). That B might be negative? (m - k)/2 is non-negative because m >= k, so >=0. It could be zero. And also we must ensure that B is integer; it is. Also check that this B yields the correct difference: |B| = (m - k)/2, |B-m| = |(m - k)/2 - m| = | - (m + k)/2| = (m + k)/2. Then | |B| - |B-m| | = |(m - k)/2 - (m + k)/2| = | -k| = k. Yes. So works. For m negative, similar.
Thus condition is sufficient.
Now, is it also necessary? Suppose there exists B integer such that | |B| - |B-m| | = k. We need to deduce that |m| >= k and (|m| - k) even. Could there be a scenario where these conditions are not necessary? Possibly if B is chosen outside the interval between 0 and m? But we already analyzed: For m>0, if B >= m, then difference = m (so k = m, then m >= k and m-k =0 even). If B <= 0, difference = m, same. If 0 <= B <= m, then difference = |2B - m| = k, so then m and k have same parity because 2B - m and m differ by even number? Actually 2B - m ≡ -m (mod 2) since 2B is even, so 2B - m ≡ m (mod 2)? Wait, 2B is even, so 2B - m ≡ -m (mod 2) ≡ m (mod 2) because -m ≡ m mod 2. So k = |2B - m| has same parity as m. So k ≡ m (mod 2). Also since |2B - m| ≤ m (because B in [0,m]), we have k ≤ m. So indeed m >= k and (m - k) even because both m and k have same parity, so m - k even. So necessary.
If m=0, then only k=0 works.
Thus condition is necessary and sufficient.
Thus answer is correct.
Now we need to output for each test case the minimum number of steps.
Implementation: read t, loop, read n,k, compute ans as described.
But careful: The input example formatting: It shows input as:
6
4 0
5 8
0 1000000
0 0
1 0
1000000 1000000
So we just read.
Thus final code.
But wait, is there any scenario where n is negative? No, n is given as 0 ≤ n ≤ 10^6. So fine.
Now, let's test with some random values using brute for small numbers to confirm.
I'll do a mental brute: For each m from 0 to maybe 5, list achievable k values:
m=0: k=0
m=1: k=1
m=2: k=0,2
m=3: k=1,3
m=4: k=0,2,4
m=5: k=1,3,5
m=6: k=0,2,4,6
So indeed parity matches m.
Now, given n, k, we want minimal steps to any m with those k achievable.
If n=3,k=2: m must be even and >=2. Options: 2 (distance 1), 4 (1), 0? 0<2 not allowed because m>=k? Actually m=0 gives |0|=0 <2, so invalid. So m=2 or 4, distance 1. So answer 1. Our formula: n>=k? 3>=2 yes, (3-2)=1 odd =>1. Good.
If n=2,k=3: m must be odd and >=3. Options: 3 (distance 1), 5 (3), etc. So answer 1. n<k? 2<3 -> ans =1. Good.
If n=2,k=4: m even >=4: m=4 distance 2, m=6 distance 4. So answer 2. n<k? 2<4 -> ans=2. Good.
If n=4,k=2: m even >=2: m=2 (2), m=4 (0), so answer 0 because m=4 works. Our formula: n>=k? 4>=2 yes, (4-2)=2 even =>0. Good.
If n=5,k=2: m even >=2: m=4 (distance 1), m=6 (1). So answer 1. n>=k? 5>=2 yes, (5-2)=3 odd =>1. Good.
If n=5,k=3: m odd >=3: m=5 (0) => answer 0. n>=k? 5>=3 yes, (5-3)=2 even =>0.
If n=5,k=4: m even >=4: m=4 (1),6(1). So answer1. (5-4=1 odd) =>1.
If n=5,k=5: answer0.
If n=5,k=6: n<k ->1? Actually 5<6, ans =1. Check m=6 even >=6? 6 even, k=6 even? parity: (6-6)=0 even, condition holds. distance 1. Good.
If n=5,k=7: n<k ->2. m=7 odd >=7, distance 2. Good.
If n=5,k=8: n<k ->3.
If n=5,k=9: n<k ->4. m=9 odd? 9 odd, parity matches, distance 4. Check: m=9, need B? B=0 gives 9, works.
Now what about n=10,k=1: n>=k, (10-1)=9 odd ->1. m odd >=1: m=9 (1), 11 (1). So answer1. Could there be m=10? 10 even not allowed. So yes.
Now, what about n=10,k=2: (10-2)=8 even ->0? m even >=2, m=10 even, so 0 steps. Works.
Now, consider n=10,k=3: (10-3)=7 odd ->1. m odd >=3: m=9 or 11, distance1.
Now, n=10,k=4: (10-4)=6 even ->0 (m=10 even, works? m=10 even, k=4 even? (10-4)=6 even, condition holds. But is k=4 achievable with m=10? Need B integer such that ||B|-|B-10||=4. For m=10, possible B? B=3 gives |3-7|=4? Check: |3|-|3-10|=3-7=-4 abs4. Yes. So works. So answer0.
Now, n=10,k=10: answer0.
Now, n=10,k=11: n<k ->1. m=11 odd, works.
Now, consider n=1,k=0: (1-0)=1 odd ->1. m even >=0: m=0 (distance1) or 2 (1). So answer1.
Thus all consistent.
So answer is simple.
Now, confirm the sample: matches.
Thus we can code.
But wait, there is a subtlety: In the problem statement, they say "increase or decrease the coordinate of A by 1" per step. So we can change A's coordinate to any integer, but the number of steps is |m - n|. That's what we used.
Now, check if the answer can be 0 when n = 0 and k = 0? Yes.
Thus implementation.
Now, before finalizing, think about potential pitfalls: Data type: n,k up to 10^6, difference up to 10^6, fits in int.
Now, maybe there is a trick: The answer for n >= k and parity mismatch is always 1, but could it be 0 if we can choose B such that the difference equals k without moving A? That is when condition holds for original n. That's when n >= k and (n - k) even. So we covered.
But is it possible that original n does not satisfy condition but there exists B such that the condition holds without moving A, even if n < k? No, because we argued that max difference is n, so if n < k, impossible. So must move.
Thus answer stands.
Now, we should produce the solution in Python or C++.
Now, maybe we should also consider that B must be integer, but we already accounted.
Now, done.
But wait, the problem statement says: "We have a point A with coordinate x = n on OX-axis. We'd like to find an integer point B (also on OX-axis), such that the absolute difference between the distance from O to B and the distance from A to B is equal to k." The figure presumably shows O at 0, A at n, B somewhere. They gave example: For n=4,k=0, B=2 gave |(2-0)-(4-2)| = 0. They didn't use absolute on B and B-n separately; they used (2-0) and (4-2) implying B is between 0 and 4. That's consistent because they used coordinate differences directly. Actually if B is between 0 and n, then OB = B (since B positive) and AB = n - B. So the difference is |B - (n - B)| = |2B - n|. So that yields condition. That's same as our region.
Thus all good.
Now, one more nuance: Could B be on the negative side? In the sample they used B=2 which is positive. But could B negative help? Possibly for some m, but again we have covered all.
Thus final answer.
Now, implement solve().
Edge Cases:
- t=0? Not according to constraints (>=1). So fine.
Now, provide reasoning in final answer.
Let's produce final output.
I'll also add comments to code.
Time complexity O(t). ```python
import sys
def solve() -> None:
data = sys.stdin.read().strip().split()
if not data:
return
it = iter(data)
t = int(next(it))
out_lines = []
for _ in range(t):
n = int(next(it))
k = int(next(it))
if n >= k:
# need m >= k and (m - k) even
if (n - k) % 2 == 0:
out_lines.append('0')
else:
out_lines.append('1')
else:
# must increase at least to k
out_lines.append(str(k - n))
sys.stdout.write('\n'.join(out_lines))
if name == "main":
solve()