We need to interpret the problem. There is a robot starting at (0,0) initially facing the positive X-axis (i.e., to the right). It can only perform moves of two types: turn right and make a step forward, or turn left and make a step forward. So each move consists of a turn (left or right) and then a step forward. The robot presumably always moves one unit after turning. The question: how many moves does it take to reach a given point (x, y) by a shortest path? But there is a constraint: the robot can only turn and then step. That means it cannot go straight without turning? Actually initially it is facing a direction, and to move, it must either turn left or right (i.e., change direction by 90 degrees) and then step forward. But what about the first move? Initially facing X-axis. For the first move, it can still perform a turn: it can turn left or right, then step. That would mean its first step is either in the positive Y direction (if turn left from facing +X? Let's define: facing +X. Turn left: rotating 90 degrees counterclockwise yields facing +Y. Turn right: rotating 90 degrees clockwise yields facing -Y. So first step can go up or down. So it cannot step in the X direction initially because that would require no turn. So the robot's path is comprised of steps that always change direction by +/- 90 degrees from previous direction. Essentially it's a path on grid, moving orthogonal steps, with the constraint that consecutive steps must be perpendicular (no straight moves). Initially it is facing east (positive X) but hasn't moved yet. The first move must be perpendicular to east, so it goes either north (0,1) or south (0,-1). After that, each next step must be perpendicular to the previous step direction, so it's like a Manhattan path that alternates between horizontal and vertical moves. However note that because you turn left or right, you could also go east/west after a vertical move. So indeed the path is an alternating sequence: vertical, horizontal, vertical, horizontal, ... (starting with vertical because initial direction is horizontal). But what about if initial direction is X-axis, so the first move is forced to be vertical. So all paths must start with a vertical step. Then after that, we must alternate: horizontal, vertical, horizontal, etc.
Now we need the shortest such path to reach (x, y). Since we can only move in perpendicular steps alternating, the shortest path length (in number of moves) might not equal Manhattan distance because you may need extra moves to adjust parity or something. Also note that we can choose directions (north vs south, east vs west) each time. And we can also choose left vs right which affects orientation but not position. So we need to find the minimal number of steps (moves) such that by an alternating sequence (starting vertical) we end at (x, y). Each step moves one unit.
We can think of it as: after n moves, we will have some coordinates (x, y). Let's denote moves: move1, move2,..., move_n. For odd moves (1,3,5,...) they are vertical; for even moves they are horizontal, because initial direction is horizontal and first turn must be vertical. After each move, the direction toggles between horizontal and vertical.
Let’s formalize: Let d0 = east (horizontal). After move i, direction becomes something; we don't care. The moves: i=1: vertical; i=2: horizontal; i=3: vertical; etc.
So let v_i = ±1 for vertical moves (positive north, negative south); h_i = ±1 for horizontal moves (positive east, negative west). Then after n moves, total x = sum of horizontal moves (i even up to n if n even, else only up to n-1), total y = sum of vertical moves (i odd up to n). Actually careful: For n moves, the moves are: move1 vertical, move2 horizontal, move3 vertical, move4 horizontal, ... So if n is odd, then there are (n+1)/2 vertical moves and (n-1)/2 horizontal moves. If n is even, then n/2 vertical and n/2 horizontal moves? Check: n=2: moves: vertical, horizontal → 1 vertical, 1 horizontal → yes equal. n=4: moves: vertical, horizontal, vertical, horizontal → 2 vertical, 2 horizontal → yes equal. So vertical moves = ceil(n/2), horizontal moves = floor(n/2). Actually for odd n, vertical = (n+1)/2, horizontal = (n-1)/2.
We need to achieve given x and y. Since steps are unit steps, the Manhattan distance |x| + |y| is the minimal number of steps if no constraints. But here we have the alternating constraint and starting vertical. So sometimes Manhattan distance may not be achievable because parity or sequence constraints? Let's explore.
We need minimal n such that there exist signs and sequence lengths (but each move is just ±1, we cannot do multiple steps in same direction without turning; we must alternate). So the only variables are the signs (direction) of each step. So the set of reachable points after exactly n moves is:
Let k = number of vertical moves = ceil(n/2), m = number of horizontal moves = floor(n/2). Then y = sum_{i=1}^{k} v_i, where each v_i = ±1. So y can be any integer with same parity as k? Actually sum of k values each ±1 yields an integer y such that |y| ≤ k and y ≡ k (mod 2)? Because sum of k numbers each ±1 = (number of +1) - (number of -1) = 2*(# of +1) - k. So y = k - 2a where a = number of -1? Let's compute: if we have p pluses and q minuses, p+q=k, y=p-q. So p=(k+y)/2, q=(k-y)/2 must be nonnegative integers, so k+y even and |y|≤k. So y must satisfy: k-|y| even? Actually k+y even is equivalent to y ≡ k (mod 2). Since k has parity, y must have same parity as k. Also |y|≤k.
Similarly, x = sum of m horizontal steps, each ±1. So |x| ≤ m and x ≡ m (mod 2) (i.e., parity condition).
Additionally, we can choose signs arbitrarily independent of each other.
Thus for given n, reachable points (x, y) are those satisfying:
|x| ≤ floor(n/2) and (floor(n/2) - |x|) even? Actually condition x ≡ floor(n/2) (mod 2) (i.e., parity). And similarly |y| ≤ ceil(n/2) and y ≡ ceil(n/2) (mod 2).
But careful: For n even: k=m=n/2. Then conditions: |x|≤n/2, x ≡ n/2 (mod 2) i.e., x and n/2 same parity. |y|≤n/2, y ≡ n/2 (mod 2). For n odd: k=(n+1)/2, m=(n-1)/2. Conditions: |x|≤ (n-1)/2, x ≡ (n-1)/2 (mod 2); |y|≤ (n+1)/2, y ≡ (n+1)/2 (mod 2).
We need minimal n such that these hold.
But also note that we can also have the possibility to turn left or right, but that only affects orientation, not the set of reachable positions? Since each move is just a step in some direction perpendicular to previous one, but as long as we can choose any sign for each vertical/horizontal step independently, is that true? Consider after a vertical move up, the robot's direction becomes either? Actually after a vertical move, the robot is facing either north or south depending on which direction it moved? Wait: The robot turns left or right before stepping. So its direction after the move is the direction it stepped in. For example, initial direction east. If it turns left, it faces north, then steps forward => moves north. After that, it is facing north. For the next move, it must turn left or right relative to north. Turning left from north gives west, turning right gives east. So the next step could be west or east. So indeed the horizontal step can be either east or west, regardless of previous vertical direction? Actually if it went north, turning left yields west, turning right yields east. So yes, you can choose either east or west independently. Similarly, if it went south initially, turning left from south gives east? Let's check: facing south, left turn is east? Standard orientation: If facing south (down), turning left (counterclockwise) would face east? Actually standard math: angles: east 0°, north 90°, west 180°, south 270°. Left turn is +90°, right turn is -90°. So if facing south (270°), left turn: 270+90=360° -> 0° (east). Right turn: 270-90=180° (west). So indeed after moving south, the next horizontal step could be east or west. So regardless of vertical direction, the horizontal step can be chosen east or west independently. Similarly, after a horizontal step, vertical step can be north or south independently. So signs are independent. So indeed the reachable set for a given n is as described.
Thus the problem reduces to: find minimal n (non-negative integer? n>=? obviously must be at least? If x=y=0? Starting point is (0,0). Robot hasn't moved; number of moves =0. But testcases might include (0,0)? Possibly but constraints allow -10^9 to 10^9, but not specified if both zero. If (0,0), answer should be 0? But we should consider if robot needs to do any moves. Since it's already there, maybe 0 moves. But the problem states: "number of moves they’ll do to reach corresponding table". So if at start, 0 moves. However we need to confirm if the initial position counts? Likely yes. But also note that the robot initially looks in direction of X-axis, but hasn't moved. So answer for (0,0) would be 0. But check constraints: x and y can be 0. We'll handle that.
Now for nonzero points, we need minimal n.
Observation: The Manhattan distance D = |x|+|y|. For unrestricted grid, minimal steps = D. But here we have alternating constraint and initial orientation. So what's the minimal n?
Let's try small examples: (3,3) output 6. D=6. So n=6 works. (3,4) output 7, D=7. So n=7 works. For (1,0): what would be answer? Starting at (0,0). To get to (1,0) (positive x). We need to have horizontal steps. But first move must be vertical. So you can't go directly horizontal. Sequence: Move1: vertical (say up) -> (0,1). Move2: horizontal (east) -> (1,1). Move3: vertical (down) -> (1,0). So that's 3 moves. D=1, but answer maybe 3. Let's test if there is shorter? n=1: only vertical -> can't get x nonzero. n=2: vertical then horizontal -> (some x, some y). Could we get (1,0)? After 2 moves, y = ±1, x = ±1, so not (1,0) because y nonzero. So minimal n=3. Check conditions: n=3 odd => k=2 vertical, m=1 horizontal. Need |y|≤2, y≡2 mod2 => y even? 0 even, and 0≡2 mod2? 2 mod2 =0, so y=0 satisfies parity: y=0 even, but parity condition says y must have same parity as k=2, i.e., even. Yes, y=0 even, ok. |y|≤2, ok. x: m=1 => need |x|≤1, x≡1 mod2 => x odd. x=1 odd, ok. So n=3 works. So answer would be 3, which is D+2? Actually D=1, so extra 2.
(0,1): point directly north. D=1. But first move must be vertical, so we can get (0,1) in 1 move: turn left and step forward => north. So answer 1. Check condition: n=1 odd => k=1, m=0. y: |y|≤1, y≡1 mod2 => y odd. y=1 odd, ok. x: m=0 => need |x|≤0 so x=0, condition satisfied (0 ≡ 0 mod2 trivially? Actually for m=0, the parity condition x ≡ m mod2 => x ≡0 mod2; 0 is even, so okay). So n=1 works.
(2,0): D=2. But can we get (2,0) in 2 moves? n=2 even => k=1, m=1. y: |y|≤1, y≡1 mod2 => y odd. y=0 is even, fails parity. So n=2 fails. n=3? n=3 odd => k=2, m=1. y: |y|≤2, y≡2 mod2 => y even => y=0 ok. x: |x|≤1, x≡1 mod2 => x odd. x=2 fails because |x|>1? Actually |x|≤1, but x=2 >1, so fails. n=4: n=4 even => k=2,m=2. y: |y|≤2, y≡2 mod2 => y even => 0 ok. x: |x|≤2, x≡2 mod2 => x even => 2 ok. So n=4 works. So answer would be 4. So (2,0) takes 4 moves. Sequence? One possible: up, right, down, left? That would return to (0,0). Actually up (0,1), right (1,1), down (1,0), left (0,0) no. Let's construct: want to end at (2,0). Could be: up (0,1), right (1,1), down (1,0), right? But after down, direction is south? Actually after down, we are facing south? Then turning left or right yields east or west. Could step right (east) to (2,0). But sequence: move1: up -> (0,1); move2: right -> (1,1); move3: down -> (1,0); move4: right? But after move3, direction is down. Turn left from down -> east, step -> (2,0). That's valid: moves: U, R, D, R. That yields (2,0). So 4 moves.
Thus minimal n is not simply Manhattan distance; it depends on parity and constraints.
We need to compute minimal n efficiently for up to 10^5 test cases with coordinates up to 1e9. So we need a formula.
Let's denote a = |x|, b = |y| (absolute values). Since signs don't matter (we can choose directions accordingly), we can assume x,y non-negative w.l.o.g. because we can flip signs by choosing directions accordingly. So we need minimal n such that there exists integers m = floor(n/2), k = ceil(n/2) with conditions:
if n even: n=2t, then k=t, m=t. Conditions: a ≤ t, and a ≡ t (mod 2); b ≤ t, and b ≡ t (mod 2).
if n odd: n=2t+1, then k=t+1, m=t. Conditions: a ≤ t, a ≡ t (mod 2); b ≤ t+1, b ≡ t+1 (mod 2).
But careful: For odd n, vertical count is t+1, horizontal count is t.
Alternatively, we can think of n as total moves. We need to find minimal n.
Let's try to characterize reachable points after n moves: The Manhattan distance D = a+b. Since each move contributes 1 to Manhattan distance (since step length 1), the total Manhattan distance after n moves is exactly n, because each step adds 1 to absolute distance? Actually careful: Manhattan distance from start is not necessarily equal to number of steps if you backtrack? But Manhattan distance = |sum of horizontal| + |sum of vertical|. However each step contributes 1 to either x or y magnitude but the signs can cancel. So the Manhattan distance of the final point is ≤ n, because steps can cancel each other. So to achieve (a,b), we need n ≥ a+b? Not necessarily; because you could overshoot and then come back? But the final Manhattan distance must be less or equal to n, but to achieve exactly (a,b) we need to have net horizontal displacement a and net vertical displacement b, which may be smaller than n due to cancellations. However n must be at least a+b because each step contributes at most 1 to the net displacement (since each step changes either x or y by ±1, the absolute net displacement in that coordinate is at most the number of steps in that axis, but you can have cancellations. However to have net a in x, you need at least a steps in x direction overall, but you could have extra steps that cancel. So n must be at least a+b, but also must satisfy parity and the alternating constraints.
In our earlier examples: (1,0) a=1,b=0: a+b=1, minimal n=3. So n > a+b. (2,0): a+b=2, n=4. (3,3): a+b=6, n=6. (3,4): a+b=7, n=7. So sometimes n equals a+b, sometimes larger.
Let's analyze patterns. Essentially we have to assign signs to steps to achieve net displacement a and b, while number of steps in each direction (vertical/horizontal) are constrained: vertical steps count = ceil(n/2), horizontal = floor(n/2). Let V = number of vertical steps, H = number of horizontal steps. Then we have:
We need to select numbers: let p_v = number of vertical steps in positive direction (say north), n_v = number in negative direction; p_v + n_v = V; p_v - n_v = b (or -b depending sign but we assume b≥0). So p_v = (V+b)/2, n_v = (V-b)/2 must be integers ≥0. So V >= b and V ≡ b (mod 2). Similarly, H >= a and H ≡ a (mod 2).
So conditions: V ≥ b and V ≡ b mod 2; H ≥ a and H ≡ a mod 2. Additionally, V and H are related by n: if n even: V=H=n/2; if n odd: V= (n+1)/2, H=(n-1)/2.
Goal: minimize n = V+H.
Thus we need to find integers V, H (non-negative integers) satisfying:
- V >= b, V ≡ b (mod 2)
- H >= a, H ≡ a (mod 2)
- |V - H| ≤ 1? Actually V and H must be either equal (if n even) or V = H+1 (if n odd). Because V = ceil(n/2), H = floor(n/2). So V = H or V = H+1.
Thus we need to find minimal total n = V+H such that these conditions hold and V,H satisfy that adjacency constraint.
We can think of it as: For given a,b, we need to choose integer t (maybe half of n) such that either:
Case 1: n even = 2t, then V=H=t. Conditions: t >= max(a,b) and t ≡ a (mod 2) and t ≡ b (mod 2). That implies a and b must have same parity as t, so a ≡ b (mod 2). So for even n, we need a and b to have the same parity (both even or both odd), and t >= max(a,b) and t has that parity. Minimal t is the smallest integer >= max(a,b) that has parity matching a (and b, since they match). So t = max(a,b) if max(a,b) parity matches a, else max(a,b)+1. Then n = 2t.
Case 2: n odd = 2t+1, then V = t+1, H = t. Conditions:
- V >= b => t+1 >= b => t >= b-1.
- H >= a => t >= a.
- V ≡ b (mod 2) => (t+1) ≡ b (mod 2) => t ≡ b-1 (mod 2).
- H ≡ a (mod 2) => t ≡ a (mod 2).
So t must satisfy t >= max(a, b-1) and t ≡ a (mod 2) and t ≡ b-1 (mod 2). Therefore a and b-1 must have same parity (i.e., a ≡ b-1 mod 2) for consistency. That is equivalent to a and b have opposite parity? Since b-1 ≡ b+1 mod2? Actually parity: b-1 mod2 = (b mod2) XOR 1? Because subtracting 1 flips parity. So condition a ≡ b-1 (mod 2) means a and b have opposite parity. Because if b is even, b-1 is odd → a must be odd. If b is odd, b-1 is even → a must be even. So a and b opposite parity. So for odd n, we need a and b opposite parity.
Additionally t must satisfy t >= max(a, b-1) and have parity equal to a (and b-1). So minimal t is the smallest integer >= max(a, b-1) that has parity equal to a (which automatically ensures parity equal to b-1 because they match given condition). If a and b opposite parity, then such t exists.
Then n = 2t+1.
Now we have two candidate n (if both parity conditions are possible) and we take the minimum. But note: If a=b=0, then both parity conditions? a=b=0 both even, so even case: t >= 0, parity: t even. Minimal t=0 => n=0. Odd case: require a and b opposite parity -> 0 and 0 are same parity, not opposite, so odd not possible. So answer 0. Good.
Now for (1,0): a=1,b=0. Parity: a odd, b even -> opposite parity? Yes, 1 and 0 are opposite. So odd case possible. t >= max(a, b-1)=max(1, -1)=1. t must have parity a=1 (odd). t=1 works. Then n=2*1+1=3. Even case requires a and b same parity, but they are opposite, so not possible. So answer 3. Good.
For (2,0): a=2,b=0. Same parity (both even). Even case: t >= max(2,0)=2, t even. t=2 gives n=4. Odd case requires opposite parity, not satisfied. So answer 4. Good.
For (3,3): a=3,b=3, same parity (both odd). Even case: t >= 3, t odd. t=3 gives n=6. Odd case requires opposite parity -> not satisfied. So answer 6, matches.
For (3,4): a=3,b=4, opposite parity (3 odd, 4 even). So odd case: t >= max(3, 4-1=3)=3, t parity a=3 odd => t=3 gives n=7. Even case not possible. So answer 7 matches.
Thus the minimal n is given by:
Let a = |x|, b = |y|.
If a == 0 and b == 0: answer 0.
Otherwise, compute candidate for even n if a%2 == b%2:
t = max(a, b)
if t%2 != a%2: t += 1
n_even = 2t
Else n_even = INF (or not applicable).
Compute candidate for odd n if a%2 != b%2:
t = max(a, b-1) # note b-1 could be negative if b=0? Let's handle.
Actually b-1 could be -1 if b=0. Then max(a, -1) = a. But condition t >= max(a, b-1) is fine; but need also t >=0. However we are taking t as smallest >= that bound with parity a. For b=0, b-1 = -1, so lower bound is a. That works. However need to ensure that V = t+1 >= b. Since b=0, t+1 >=0 always true. And H = t >= a, so t>=a is enforced. Also need t >=0. For a>0, t will be >=a>0. For a=0,b>0? Let's test a=0,b=1: a=0,b=1 opposite parity. Then t >= max(0, 1-1=0)=0, parity a=0 even. Minimal t=0 gives n=1. That's correct (point (0,1) answer 1). So t=0 works. For a=0,b=0 we already handled.
Thus t = smallest integer >= max(a, b-1) such that t%2 == a%2.
Then n_odd = 2t + 1.
Answer = min(n_even, n_odd) if both exist, else whichever exists.
We must ensure that t from odd case is indeed feasible regarding V>=b and H>=a, parity: already satisfied by construction. Also need to consider that V = t+1 must be >= b; since t >= b-1 ensures t+1 >= b. Good. H = t >= a ensures H>=a. So fine.
Now we can compute n efficiently.
But is there a simpler closed form? Possibly the answer equals max(2max(a,b) - (a+b)%2? Let's test. For (3,3): max=3, 23=6, (a+b)%2=0, so 6-0=6. For (3,4): max=4, 24=8, (a+b)%2=1, 8-1=7? 8-1=7 yes. For (2,0): max=2, 22=4, (a+b)%2=2%2=0, 4-0=4. For (1,0): max=1, 21=2, (a+b)%2=1%2=1, 2-1=1? But answer is 3, not 1. So that formula fails for (1,0). Wait (a+b)%2=1, 21-1=1, but answer is 3. So need to adjust. Perhaps answer = max(2*max(a,b) - ((a+b)%2) ?) But (1,0) gives 1, wrong.
Maybe answer = 2*max(a,b) - ( (a+b) % 2 ) + something if max(a,b) is less than something? Let's derive properly.
We need to compute minimal n. Let's consider two cases based on parity of a and b.
Case 1: a and b have same parity.
Then answer = 2 * max(a, b) if max(a,b) has same parity as a (i.e., parity condition)? Actually t = max(a,b) if its parity matches a (and b); else t = max(a,b)+1. So n = 2t = 2max(a,b) if max(a,b)%2 == a%2; else 2*(max(a,b)+1) = 2max(a,b)+2.
But note that if a and b have same parity, then max(a,b) has the same parity as a only if max(a,b) is congruent to a mod2. Since a and b same parity, both are either even or odd. If max is odd when both odd, then parity matches; if max is even when both even, matches. Actually if both are even, max is even, parity matches. If both odd, max is odd, parity matches. So max(a,b) always has same parity as a and b because if both are odd, max is odd; if both even, max is even. So t = max(a,b) always satisfies parity? Let's test: a=2,b=4 both even, max=4 even, matches. a=4,b=2 both even, max=4 even matches. a=1,b=3 both odd, max=3 odd matches. So indeed when a and b share parity, max(a,b) has that parity. So t = max(a,b) works. But wait, there is nuance: if a and b are same parity, it's not guaranteed that max(a,b) has the same parity as both? Yes, because both have parity p; any number that is either a or b will have parity p. So max will be either a or b, which both have parity p. So indeed max(a,b) automatically has parity p. So t = max(a,b) always satisfies parity condition. Then n_even = 2max(a,b). However check (a,b) = (0,0): max=0, n=0 works. (a,b)=(2,0): max=2, n=4 works. (a,b)=(3,3): max=3, n=6 works. (a,b)=(4,2): max=4, n=8? But is that minimal? Possibly not, maybe odd n could be smaller? But same parity excludes odd case? But could there be an odd n that works even though parity differs? For same parity, odd n condition requires opposite parity, so no odd candidate. So answer = 2max(a,b). Wait but test (a,b) = (0,2): max=2, n=4. But earlier we thought (0,2) might be reachable? Let's test (0,2): point north two units. Manhattan distance=2. But can we reach (0,2) in 2 moves? n=2 even: V=1,H=1. y: need |y|≤1? Actually V=1, need b=2? b=2 > V=1, so not reachable. So need n≥? Let's try n=3 odd: V=2,H=1. Conditions: V>=b=2, V≡b mod2? 2≡2 ok. H>=a=0 ok. So n=3 would work? But parity: a=0,b=2 same parity (both even). Odd case requires opposite parity, so n=3 is not possible? Let's check directly: n=3 odd => V=2,H=1. b=2 <=2 okay. But need b≡V mod2? V=2 even, b=2 even, okay. a=0 <=1, a≡H mod2? H=1 odd, a=0 even -> not satisfied. So indeed n=3 fails because a (0) must have same parity as H=1. So need H even? Not possible with n=3. So maybe n=4? V=2,H=2: b=2<=2, parity okay; a=0<=2, a even, H even (2) -> ok. So n=4 works. So answer 4. 2max=2*2=4, correct.
Now test (a,b) = (1,1): both odd, max=1, n=2? But is (1,1) reachable in 2 moves? n=2 even: V=1,H=1. Conditions: b=1<=1, b≡V mod2: 1≡1 ok. a=1<=1, a≡H mod2:1≡1 ok. So reachable: sequence? Start: vertical up (0,1), horizontal right (1,1) -> that's 2 moves. So answer should be 2. Our formula gives 2*max(1,1)=2, correct.
So for same parity, answer = 2 * max(a,b). That seems to hold.
Now case 2: a and b opposite parity.
Then even n is impossible. So only odd n candidate. We need to compute minimal odd n.
n_odd = 2*t+1, where t is the smallest integer >= max(a, b-1) with t%2 == a%2. Since a and b opposite parity, we have a%2 != b%2. Let's try to derive a closed form.
We want minimal t such that t >= L = max(a, b-1) and t ≡ a (mod 2). Since L might be less than or equal to something.
Let L = max(a, b-1). Then t = L if L%2 == a%2 else L+1.
Then n = 2t + 1 = 2L + 1 if L%2 == a%2 else 2*(L+1)+1 = 2L+3.
Now we need to see if this can be expressed in terms of a and b.
First, compute L = max(a, b-1). Note b-1 could be less than a. Since a and b opposite parity, and b is integer.
Consider two subcases:
- a >= b-1, i.e., a >= b-1 → a+1 >= b? Actually a >= b-1 means a+1 >= b. So b <= a+1.
Then L = a.
Now check parity: L = a, its parity is a%2, which matches a%2 obviously. So t = a works (since L%2 == a%2). Then n = 2a + 1.
But is this always valid? Need also V = t+1 >= b? t+1 = a+1 >= b? Since b <= a+1, this holds. Also H = t = a >= a holds. So n = 2a+1 works when b <= a+1 and a,b opposite parity. Let's test: (a,b) = (1,0): a=1,b=0, b <= a+1? 0 <=2 true, opposite parity. n=21+1=3 correct. (a,b)=(2,1): a=2,b=1 opposite? 2 even,1 odd yes. b <= a+1? 1 <=3 true. Then n=22+1=5. Check manually? (2,1) Manhattan distance 3. Could there be a shorter? Let's test n=3 odd: V=2,H=1. Conditions: b=1<=2 ok, V parity:2 even vs b=1 odd? V≡b? 2≡1 mod2? No, so fails. n=5? n=5 odd: V=3,H=2. b=1<=3, V parity:3 odd matches b=1 odd. a=2<=2, H parity:2 even matches a=2 even. So works. n=5. Could n=4 even? Parity same? a=2 even, b=1 odd -> not same parity, so no. So answer 5. So formula works. But maybe n=4? Even not possible. So 5 seems minimal. Is there any chance n=3? No. So ok.
Now consider subcase 2: b-1 > a, i.e., b-1 > a → b >= a+2. Since b and a opposite parity, b >= a+2 is possible. Then L = b-1.
Now L = b-1. We need t = L if L%2 == a%2 else L+1.
But note parity relation: Since a and b opposite parity, a%2 = 1 - b%2. L = b-1: parity of b-1 is opposite to parity of b (since subtracting 1 flips parity). So (b-1)%2 = 1 - b%2. So L%2 = 1 - b%2. But a%2 = 1 - b%2 as well (since opposite). Thus L%2 equals a%2 automatically! Because a%2 = 1 - b%2 = (b-1)%2. So L%2 == a%2 always holds. Therefore t = L = b-1 works. Then n = 2*(b-1) + 1 = 2b - 1.
But need also check H = t = b-1 >= a? Since we are in case b-1 > a, i.e., b-1 >= a+1? Actually condition b-1 > a implies b-1 >= a+1? Since integers, b-1 > a means b-1 >= a+1. So t = b-1 >= a+1 > a, so H >= a+1 > a, so condition H>=a satisfied. Also V = t+1 = b >= b holds. So n = 2b - 1 works.
Thus for opposite parity, answer = 2max(a,b) - 1? Let's test: In subcase1, when a >= b-1 (i.e., a >= b-1) and opposite parity, answer = 2a+1. Since a >= b-1, we have a ≥ b-1 → a ≥ b-1, not necessarily a ≥ b. Could be a = b-1? If a = b-1, then a and b opposite parity? If a = b-1, then parity: if b even, a odd; if b odd, a even; so opposite parity holds. Then answer = 2a+1 = 2(b-1)+1 = 2b-1. But also 2b-1 = 2max(a,b)-1? Since max(a,b) = b (because b > a if a=b-1? Actually if a=b-1, then b = a+1, so max = b). 2max-1 = 2b-1, matches. In subcase1 where a > b-1 (i.e., a >= b? Actually a >= b? Let's examine: a >= b-1. If a >= b, then max(a,b)=a. Then answer 2a+1 = 2max+1? That would be 2max+1, not 2max-1. But if a >= b, then is it possible that a and b opposite parity? Yes. Example: a=3,b=2 (opposite parity: 3 odd,2 even). Here a>=b, so max=3. Our answer = 23+1=7. But is that correct? Let's test (3,2). Manhattan distance 5. Our derived formula for opposite parity gave answer = if a >= b-1, which holds because a=3 >= b-1=1, so answer = 2a+1=7. But is there a smaller n? Let's check n=5 odd: V=3,H=2. Need b=2<=3, V parity: 3 odd vs b=2 even? No, fails because opposite parity? Actually need V ≡ b mod2; V=3 odd, b=2 even, fails. n=7 odd: V=4,H=3. V=4 even ≡ b=2 even ok. H=3 odd ≡ a=3 odd ok. So n=7 works. Could n=6 even? Even requires same parity, not opposite, so no. So answer 7. Now max=3, 2max-1 =5, which is too small; 2max+1=7 matches. So the answer in opposite parity case is either 2max+1 or 2max-1? Let's see conditions: When a >= b, we got 2a+1 = 2max+1. When b > a, we got 2b-1 = 2*max-1. So there is asymmetry. Let's check the condition more precisely:
We have two subcases derived based on L = max(a, b-1). If a >= b-1 (which includes both a = b-1 and a >= b), then answer = 2a+1. If b-1 > a (i.e., b > a+1), then answer = 2b-1.
But note that when a = b, parity same, so opposite parity not possible. So for opposite parity, a and b are not equal. So we have either a > b or a < b. Let's test a > b: then b <= a-1? Actually if a > b, then a >= b+1. Then b-1 <= a-2? Actually b-1 <= a-2? Not necessarily. But compare a and b-1: Since a > b, we have a >= b+1 > b-1, so a >= b-1+2? Actually b+1 > b-1 obviously. So a >= b+1 > b-1, so a > b-1. So condition a >= b-1 holds. So for a > b, we are in subcase1, answer = 2a+1. That gives answer = 2*max+1 because max = a.
For a < b: then we need to check if a >= b-1? If a = b-1, then a >= b-1 holds (equal). So in that borderline, answer = 2a+1 = 2(b-1)+1 = 2b-1. But also if a < b-1 (i.e., b >= a+2), then condition b-1 > a holds, answer = 2b-1. So for a < b, both possibilities yield answer = 2b-1. Because when a = b-1, answer = 2b-1 as well. So for a < b, answer = 2b-1. For a > b, answer = 2a+1.
Thus we can combine:
If a == b (same parity case already handled separately; if a==b and opposite parity impossible), but for completeness:
Let M = max(a,b). If a and b have same parity: answer = 2M.
If a and b have opposite parity:
if a > b: answer = 2a + 1 (since M = a)
else (b > a): answer = 2*b - 1 (since M = b)
But note: Could there be scenario where a > b but answer = 2M - 1? Let's test a=5,b=2: opposite? 5 odd,2 even yes. According to our rule a > b → answer = 25+1=11. Check n=9? n=9 odd: V=5,H=4. Need b=2 <=5, V parity: 5 odd vs b=2 even fails. n=11 odd: V=6,H=5. V even vs b even ok, H odd vs a odd ok. So 11 works. Could n=9? Let's check n=9 parity: V=5,H=4; V parity odd, b even fails. So answer 11. 2*max-1=9, fails. So indeed.
For a < b: a=2,b=5: answer = 2*5-1=9. Check n=9 odd: V=5,H=4. b=5 <=5 ok, V parity 5 odd vs b=5 odd ok. a=2 <=4, H parity 4 even vs a=2 even ok. So 9 works. Could n=7? n=7 odd: V=4,H=3. b=5 >4 fails. n=8 even not allowed. So answer 9. So rule holds.
Thus final formula:
Given |x| = a, |y| = b.
If a == 0 and b == 0: answer = 0.
Else:
If (a % 2) == (b % 2): answer = 2 * max(a, b)
Else:
if a > b: answer = 2a + 1
else: answer = 2b - 1 (since b > a necessarily, because parity different and can't be equal)
But wait, what about a = 0, b = 1? Opposite parity: a=0 even, b=1 odd. Here a < b, so answer = 2b -1 = 21-1=1, correct.
a=1,b=0: a > b, answer = 21+1=3, correct.
a=0,b=2: same parity (both even) -> answer = 22=4, correct.
a=0,b=3: opposite parity -> answer = 2*3-1=5. Check (0,3): n=5? n=5 odd: V=3,H=2. b=3<=3 ok, V parity 3 odd matches b=3 odd; a=0<=2, H parity 2 even matches a=0 even. So 5 works. Could n=3? n=3 odd: V=2,H=1; b=3>2 fails. So 5 correct. So formula works.
Now, we should double-check border cases like large values, negative, etc. Since we take absolute values, it's fine.
But is there any scenario where both parity same but answer is not 2max? Let's test a=2,b=4 (both even) -> 24=8. Could there be odd n smaller? n=7 odd: V=4,H=3. Conditions: b=4<=4 ok, V parity 4 even matches b even. a=2<=3, H parity 3 odd vs a even fails. So n=7 fails. n=5 odd: V=3,H=2; b=4>3 fails. So 8 is minimal. So ok.
What about a=1,b=3 (both odd) -> 2*3=6. n=5 odd: V=3,H=2; b=3<=3 ok, V parity 3 odd matches b odd; a=1<=2, H parity 2 even vs a odd fails. So 6 is minimal.
Now consider a=2,b=2 (both even) -> 2*2=4. Sequence: up, right, down, left? That returns to (0,0). Actually to get (2,2) we need: up, right, up? No, we need net y=2,x=2. For n=4 even: V=2,H=2. One possible: up (0,1), right (1,1), up (1,2), right (2,2) -> moves: vertical up, horizontal right, vertical up, horizontal right. Is that allowed? After moving right, direction is east. To move up again, you need to turn left or right. From east, left turn gives north, right turn gives south. So you can turn left to go north again. So yes sequence works. So 4 moves. So correct.
Now consider a=3,b=1 (opposite parity, a > b) -> answer = 23+1=7. Check n=5? n=5 odd: V=3,H=2; b=1<=3 ok, V parity 3 odd vs b=1 odd? Actually 3 odd, 1 odd same parity? Wait b=1 odd, V=3 odd, so parity matches. But a=3 <=2? H=2 <3, fails. So n=7 works: V=4,H=3; b=1 <=4, V parity 4 even vs b=1 odd fails? Wait careful: For n=7, V=4 (since ceil(7/2)=4), H=3. Conditions: V >= b (4>=1) and V ≡ b mod2? 4 even, b=1 odd -> fail. Oops! So maybe n=7 doesn't work for (3,1)? Let's recompute: For opposite parity, when a > b, we had derived answer = 2a+1. For a=3,b=1, that gives 7. But we just saw parity condition: V ≡ b (mod2) required. With n=7, V=4, b=1 -> 4 mod2=0, 1 mod2=1 -> not equal. So n=7 fails! Something is wrong. Let's check our derivation for opposite parity subcase1: we assumed L = a if a >= b-1, and then t = a, n=2a+1. But we must also check V parity condition: V = t+1 = a+1. Condition V ≡ b mod2. So we need (a+1) % 2 == b % 2. Since a and b opposite parity, a%2 != b%2. So (a+1)%2 = 1 - a%2 (since adding 1 flips parity). So (a+1)%2 = 1 - a%2. But b%2 = 1 - a%2 because opposite parity. So indeed (a+1)%2 = b%2. So condition holds. Good. For a=3,b=1: a%2=1, b%2=1? Wait 3%2=1, 1%2=1, they are same parity! That's not opposite parity. Actually 3 and 1 are both odd. So my example a=3,b=1 are same parity, not opposite. I mis-stated. For opposite parity, we need a%2 != b%2. So if a=3 (odd) and b=1 (odd), they are same parity. So that case belongs to same parity, answer = 2max=2*3=6. So 6 would be answer. That matches our earlier rule: both odd -> 6. Let's check if 6 works: n=6 even, V=3,H=3. Conditions: b=1<=3, b parity odd matches V=3 odd; a=3<=3, a parity odd matches H=3 odd. So 6 works. So good.
For opposite parity example a=4,b=1: a even, b odd. Then a > b, answer = 2*4+1=9. Check V = t+1 = a+1 =5, V%2 = 5%2=1, b%2=1 ok. H = t = a =4, H%2=0, a%2=0 ok. So 9 works. Could there be smaller? n=7? n=7 odd: V=4,H=3. V%2=0 vs b=1 odd fails. n=5 odd: V=3,H=2; V%2=1 vs b=1 ok? 3%2=1 matches b=1, but H=2 even vs a=4? a=4 even but need H>=a? 2<4 fails. So n=9 minimal. So correct.
Now check a=2,b=5: a even, b odd (opposite), a < b, answer = 2*5-1=9. n=9 odd: V=5,H=4; V%2=1 matches b=1? b=5 odd ok, H%2=0 matches a=2 even ok. n=7? V=4,H=3; V%2=0 vs b odd fails. So 9 works.
Now check a=1,b=4: opposite? 1 odd,4 even. a<b, answer = 24-1=7? Actually 24-1=7. Check n=7 odd: V=4,H=3; V%2=0 vs b=4 even? b=4 even, V%2=0 ok; H%2=1 vs a=1 odd ok. So 7 works. n=5? V=3,H=2; V%2=1 vs b even fails. So 7 minimal. Could n=6 even? Even requires same parity, not opposite, so not.
Now check a=5,b=2: opposite? 5 odd,2 even -> a>b, answer = 2*5+1=11. We already checked.
Thus the formula seems solid.
But wait, there is also case a=0,b=0 we handled.
Edge cases: When a=0 and b is odd, answer = 2b-1? For b=1 gives 1, b=3 gives 5, b=5 gives 9, etc. That matches our earlier checks.
When b=0 and a is odd, answer = 2a+1? For a=1 gives 3, a=3 gives 7, a=5 gives 11, etc. Yes.
Now we must ensure this formula yields minimal n. But is there any scenario where we could get n smaller than this by using a larger n that is even when parity same? That we already covered.
Now we should also consider if n=0 is only when a=b=0. So fine.
Now complexity: O(T) per test case.
But caution: The problem statement says initially they look in direction of X-axis. They can turn right and step forward, or turn left and step forward. That's exactly as we assumed. However, there might be nuance: Could the robot choose to turn multiple times without stepping? The description: "they can only do the next moves: turn right and make a step forward or turn left and make a step forward." So each move consists of a turn followed by a step. They cannot turn without stepping, nor step without turning. So our model is correct.
Now we need to output number of moves. Note that the robot might also be able to do a 180-degree turn by two turns? But each move includes exactly one turn and one step, so you cannot turn twice in place without moving. So our interpretation stands.
Now, we have derived the answer purely based on absolute values and parity.
But wait, there is also the possibility that the robot could turn left or right, which changes orientation, but we considered that signs are independent. However, is there any additional constraint linking the signs of consecutive moves? For example, after moving north, turning left yields west, turning right yields east. So you can go either east or west next. So indeed you can independently choose the sign of the next horizontal step, but is there any limitation from the previous direction that might restrict ability to achieve arbitrary signs for all steps? Let's examine sequence of signs: Suppose we have a sequence of vertical and horizontal moves. For vertical moves, the direction (north or south) can be chosen each time? For the first vertical move, we can choose north (turn left) or south (turn right). That's fine. For subsequent vertical moves, the robot is coming from a horizontal move. After a horizontal move, say we moved east. Then direction is east. To make a vertical move, we need to turn left or right. Turning left from east gives north, turning right gives south. So we can choose north or south independently of previous vertical direction. Similarly, if we moved west, turning left from west gives south? Let's check: facing west (180°), left turn (+90°) => 270° (south), right turn (-90°) => 90° (north). So still you can choose north or south. So indeed each vertical move's direction can be chosen independently as plus or minus, regardless of previous ones. Similarly, for horizontal moves, after a vertical move, you can choose east or west independently. So sign independence holds.
Thus our conditions are necessary and sufficient.
Now final formula: Let a = abs(x), b = abs(y).
If a == 0 and b == 0: ans = 0.
Else if (a % 2 == b % 2): ans = 2 * max(a, b).
Else (different parity):
if a > b: ans = 2 * a + 1
else: ans = 2 * b - 1
But careful: In the else case, is it always b > a? It could be a == b? If parity different, cannot be equal because equal numbers have same parity. So indeed either a > b or a < b.
Now we must test more random examples to ensure no off-by-one.
Test (a,b) = (0,2) -> same parity -> ans=4, we tested works.
(0,4) -> ans=8. n=8 even: V=4,H=4. b=4 ok, parity even ok. x=0<=4, parity even ok. So 8 works. Could n=6? n=6 even: V=3,H=3; b=4>3 fail. n=7 odd: V=4,H=3; b=4<=4 ok, V parity 4 even vs b even ok; a=0<=3, H parity 3 odd vs a=0 even fail. So 8 minimal. Good.
(2,1) -> parity? 2 even,1 odd opposite. Since a > b? a=2,b=1, a > b, ans = 2*2+1=5. Check n=5 odd: V=3,H=2; V parity 3 odd vs b=1 odd ok; H parity 2 even vs a=2 even ok. n=3 odd: V=2,H=1; V parity 2 even vs b=1 odd fail. n=4 even: same parity needed, not. So 5 minimal.
(1,2) -> a<b, ans=22-1=3? Actually 22-1=3. Check (1,2): n=3 odd: V=2,H=1; V parity 2 even vs b=2 even ok; H parity 1 odd vs a=1 odd ok. But H=1 >= a=1 ok. So 3 moves works. Sequence:? Start: vertical up (0,1) [choose north], then horizontal right (1,1), then vertical up (1,2) -> 3 moves. Yes, works. So answer 3, correct.
(2,3): a=2 even, b=3 odd opposite, a<b, ans=2*3-1=5. n=5 odd: V=3,H=2; V parity 3 odd vs b=3 odd ok; H parity 2 even vs a=2 even ok. n=3 odd: V=2,H=1; b=3>2 fail. So 5 minimal.
(3,2): a=3 odd, b=2 even opposite, a>b, ans=2*3+1=7. n=7 odd: V=4,H=3; V parity 4 even vs b=2 even ok; H parity 3 odd vs a=3 odd ok. n=5 odd: V=3,H=2; V parity 3 odd vs b=2 even fail. So 7 minimal.
Now consider (1000000000, 999999999): a=1e9, b=1e9-1. Parities: 1e9 is even? 1e9 = 1000000000, which is even; 999999999 is odd. So opposite parity, and a > b. ans = 2*a+1 = 2000000000+1=2000000001. That's within 32-bit? Up to 2e9+1, which fits in 64-bit. Output as integer.
Now check (1000000000, 1000000000): both even, ans=2*1e9=2000000000.
Now check (0, 1000000000): even, ans=2*1e9=2000000000.
Now check (0, 999999999): odd, opposite? Actually a=0 even, b odd -> opposite, a<b, ans=2b-1 = 2999999999 -1 = 1999999997. That seems correct.
Now we should verify with some random small points using brute force for n up to maybe 10, to see if our formula matches minimal n. Let's do mental brute for all a,b up to 5:
We can compute minimal n by checking n=0..? Use formula.
a,b: (0,0)->0
(0,1)->1 (our: opp, a<b -> 21-1=1)
(0,2)->4 (same->22=4)
(0,3)->5 (opp->23-1=5)
(0,4)->8 (same->8)
(0,5)->9 (opp->25-1=9)
(1,0)->3 (opp a>b? Actually a=1,b=0 -> a>b -> 21+1=3)
(1,1)->2 (same->21=2)
(1,2)->3 (opp a<b -> 22-1=3)
(1,3)->6? Let's compute: (1,3) a=1 odd, b=3 odd same? 1 odd,3 odd same -> ans=23=6. Check n=4? n=4 even: V=2,H=2; b=3>2 fail; n=5 odd: V=3,H=2; b=3<=3 ok, V parity 3 odd matches b; a=1<=2, H parity 2 even vs a odd fails; n=6 even: V=3,H=3; both ok parity? V=3 odd matches b=3 odd; H=3 odd matches a=1 odd? Wait H=3 odd, a=1 odd, ok; so 6 works. Could n=5 be fixed by different t? n=5 fails as above. So 6 correct.
(1,4): a=1 odd, b=4 even opp, a<b -> ans=24-1=7. Check n=5? n=5: V=3,H=2; b=4>3 fail; n=7: V=4,H=3; V even vs b even ok; H odd vs a odd ok, works.
(1,5): a=1 odd, b=5 odd same -> ans=25=10? Actually 2*5=10. Check n=9? n=9: V=5,H=4; V odd vs b odd ok; H even vs a odd fails; n=10: V=5,H=5; both odd? V=5 odd ok, H=5 odd vs a=1 odd ok, so 10 works. Could n=8? n=8 even: V=4,H=4; b=5>4 fail. So 10 minimal.
(2,0)->4 (same->22=4)
(2,1)->5 (opp a>b? 2>1 -> 22+1=5)
(2,2)->4 (same->22=4)
(2,3)->5 (opp a<b -> 23-1=5)
(2,4)->8 (same->24=8)
(2,5)->9 (opp a<b -> 25-1=9)
(2,6)->? a=2 even,b=6 even same -> 12? Actually 2*6=12. Check n=10? n=10 even: V=5,H=5; b=6>5 fail. n=11 odd: V=6,H=5; V even vs b even ok; H odd vs a even fails; n=12 even: V=6,H=6; works. So 12.
(3,0)->7 (opp a>b -> 23+1=7)
(3,1)->6 (same odd? 3 odd,1 odd same -> 23=6)
(3,2)->7 (opp a>b -> 23+1=7)
(3,3)->6 (same->6)
(3,4)->7 (opp a<b -> 24-1=7)
(3,5)->10? (3 odd,5 odd same -> 25=10)
(3,6)->? (3 odd,6 even opp, a<b -> 26-1=11) Check n=11: V=6,H=5; V even vs b even ok; H odd vs a odd ok; n=9? V=5,H=4; b=6>5 fail; n=10 even: V=5,H=5; b=6>5 fail; so 11 correct.
All match.
Thus formula seems correct.
Now we need to implement: For each test case, read x,y, compute a=abs(x), b=abs(y). If a==0 and b==0 -> print 0. Else if (a%2 == b%2) -> print 2max(a,b). Else if a > b -> print 2a+1 else print 2*b-1.
But we must be careful: For opposite parity and a < b, answer is 2b-1. However, if b=0? Then a>0 and opposite parity? If b=0, then a must be odd for opposite parity. Then a > b, so we go into a > b branch and answer 2a+1. That matches earlier. So fine.
Now consider a=0,b>0 and opposite parity: a < b, answer = 2*b-1, works.
Now verify (a,b) = (0,0) we handled.
Now we should also think about negative coordinates: absolute works.
Now double-check with example: (3,4) gave 7, matches.
Now, potential nuance: The robot initially looks in direction of X-axis. That is positive X direction. What if x is negative? It can still achieve negative x by moving west on horizontal steps. So absolute works.
Now, is there any scenario where we need to consider the robot's orientation at the end? No, we just need to reach the point.
Now, ensure that our answer never negative.
Now, let's test edge case: (1000000000, 1000000000) both even, answer 2000000000. That's within 32-bit signed? 2e9 is exactly 2,000,000,000 which is just above 2^31-1=2,147,483,647? Actually 2e9 is less than 2,147,483,647, so fits in signed 32-bit. But for safety, use 64-bit (long long). T up to 1e5, values up to 1e9, answer up to maybe 2e9+1, which fits in 32-bit signed? 2e9+1 = 2,000,000,001, which is less than 2,147,483,647, so still fits. So 32-bit int is safe? In C++, int is typically 32-bit, range -2.147e9 to 2.147e9. 2,000,000,001 < 2,147,483,647, so ok. However if a and b are both 1e9 and same parity, answer=2e9, which also fits. So int is ok. But to be safe, use long long.
Now, one more thought: The formula we derived uses the fact that the robot can choose signs arbitrarily. But is there any additional constraint from the fact that the robot's first move must be vertical? We incorporated that by requiring vertical count = ceil(n/2). That's correct.
But what about the possibility that the robot could also choose to start by turning left or right? Both are allowed, so first step can be up or down.
Thus formula is correct.
Now, let's test some random values with a small brute force program to be absolutely sure. Since I can't run code here, I'll reason more.
Consider (a,b) = (2,2) -> 4 moves. Could there be a path with 3 moves? n=3 gives V=2,H=1. Then y could be ±2,0? Actually V=2 gives possible y: -2,0,2. To get y=2, need both vertical steps up. Then H=1 gives x = ±1. So cannot get x=2. So no. So 4 is minimal.
Consider (a,b) = (4,1) -> opposite parity, a>b -> ans=2*4+1=9. Could n=7 work? V=4,H=3. b=1 <=4, V parity 4 even vs b=1 odd fail. n=8 even same parity needed, but parity opposite, so fail. So 9 minimal.
Consider (a,b) = (1,3) -> same parity -> ans=2*3=6. Already checked.
Consider (a,b) = (5,0) -> odd, a>b -> 2*5+1=11. Check n=9? n=9 odd: V=5,H=4; b=0 <=5 ok, V parity 5 odd vs b=0 even fail. n=10 even: same parity required? a=5 odd, b=0 even not same, so fail. So 11 minimal.
Consider (a,b) = (0,5) -> odd, a<b -> 2*5-1=9. n=7 odd: V=4,H=3; b=5>4 fail. n=8 even: same parity? a=0 even, b=5 odd not same, so fail. So 9 minimal.
Now, what about (a,b) = (1000000000, 999999998)? Both even? 1e9 even, 999999998 even -> same parity -> ans = 2*max=2e9. Works.
Now, what about (a,b) = (999999999, 1000000000)? a odd, b even opposite, a<b -> ans=2*1000000000-1=1999999999. That is less than 2e9. Good.
Now, confirm that our answer is always >= Manhattan distance. For same parity: ans=2max. D = a+b. Since max >= (a+b)/2? Actually 2max >= a+b? For two numbers, max >= (a+b)/2? Not always; e.g., a=100,b=0: 2max=200, D=100, so ans=200 > 100. For a=2,b=2: 2max=4, D=4, equal. For a=3,b=1: same parity? Actually 3 odd,1 odd same parity, ans=2*3=6, D=4, ans> D. So indeed ans >= D, with equality when? When a=b and parity same, ans=2a, D=2a, so equality. Also when one is zero? If b=0, a even, ans=2a, D=a, ans > D. So only case equality is when a=b (and parity same, which is automatically if a=b, they have same parity). So only points where x = ±y and |x| = |y| (diagonals) have ans = Manhattan distance. Also (0,0) 0.
For opposite parity, ans = either 2max+1 or 2max-1. Compare to D: D = a+b. If a > b, ans = 2a+1, D = a+b <= a+a? Since b < a, D < 2a. So ans = 2a+1 > 2a >= D+? Usually > D. If a < b, ans = 2b-1, D = a+b < b+b = 2b, so ans = 2b-1 could be close to D. For example (1,2): ans=3, D=3 equality. For (2,3): ans=5, D=5 equality. For (2,5): ans=9, D=7, ans > D. So when does equality hold for opposite parity? When a < b and ans = 2b-1, D = a+b. Equality 2b-1 = a+b => a = b-1. So if a = b-1, then D = (b-1)+b = 2b-1 = ans. Similarly for a > b and equality would require 2a+1 = a+b => b = a+1, but in that case a > b? Actually if b = a+1, then a < b, not a > b. So equality only occurs when a = b-1 (i.e., the smaller is one less than larger) and parity opposite? If a = b-1, then parity: if b is even, a = odd? b even -> a odd, so opposite parity, yes. So points where |x| and |y| differ by 1 and the larger coordinate is even? Actually if a = b-1, then b = a+1. So the larger is b, which could be even or odd? Example: (1,2): larger=2 even, ans=3=D. (2,3): larger=3 odd, ans=5=D. Wait 2b-1: if b=3, ans=5, D=5. So equality holds regardless of parity of larger? Let's check condition: a = b-1, then D = 2b-1, ans = 2b-1. So works. So equality holds when the coordinates differ by exactly 1 in absolute value (i.e., | |x| - |y| | = 1). Actually if a = b-1, then difference =1. So those points have answer = Manhattan distance. Also if a = b, answer = Manhattan distance. So the only points where answer equals Manhattan distance are when |x| = |y| or | |x| - |y| | = 1? Check (0,1): difference=1, answer=1, D=1, yes. (0,2): difference=2, answer=4, D=2, not equal. (1,3): difference=2, D=4, answer=6, not equal. (2,4): difference=2, D=6, answer=8, not equal. So only difference 0 or 1 yields equality.
Now, confirm with (2,1): difference=1, but a>b? Actually a=2,b=1, difference=1, but a > b, not b > a. In that case, we are in opposite parity? 2 even,1 odd opposite, a > b, ans=2*2+1=5, D=3, not equal. So equality only when the smaller is exactly one less than larger and the larger is the max? But we have condition a = b-1 (i.e., larger = b, smaller = a). That implies b > a. So when larger coordinate is y (if |y| > |x|) and the difference is 1, we get equality. But if larger is x and difference 1, then we have a > b and a = b+1. That would be a = b+1. In that case parity? If a = b+1, then parity opposite. But our formula for a > b gives ans = 2a+1 = 2(b+1)+1 = 2b+3, while D = a+b = 2b+1. So not equal. So equality only when the larger coordinate is even? Actually no, let's check (3,2): a=3,b=2, diff=1, a>b, ans=7, D=5, not equal. So indeed when larger is x (a) and difference 1, ans > D. So there's asymmetry due to starting vertical. Because we must start with a vertical move, so if the larger coordinate is vertical (y), it's easier? Because we have more vertical moves. Starting vertical gives an extra vertical move when n is odd? Actually recall that for opposite parity, when a < b, ans = 2b-1, which equals D when a = b-1. That means if the larger coordinate is the vertical one (y), we can achieve equality. If the larger is horizontal, we pay extra. This matches intuition: Since first move is vertical, it's beneficial if the target has larger y (vertical) displacement relative to x.
Thus formula consistent.
Now we should implement this.
But wait, there is also possibility that the robot could turn right or left, but maybe also turn around by doing two turns in a row? However each move includes exactly one turn and one step. So you cannot do two turns without stepping. So you cannot reverse direction in place; you must step after each turn. So you cannot simply do a 180 turn. So our constraints hold.
Now we need to be careful: The problem statement might be interpreted differently: "they can only do the next moves: turn right and make a step forward or turn left and make a step forward." This might mean that at each step, the robot has two choices: either turn right and then step forward, or turn left and then step forward. It cannot go straight. That is what we used.
Now, just to be thorough, let's consider if the robot could also choose not to move? No, to reach a table it must move. So fine.
Now, test a random point: (-3, -4) absolute (3,4) gives 7, same.
Now, what about (1000000000, -1000000000)? Absolute (1e9,1e9) even, ans 2e9.
Now, we should also consider if there is any special case where a=0, b=0 we output 0.
Now, implement solve():
Read T.
Loop T:
read x, y
a = abs(x)
b = abs(y)
if a==0 and b==0: print 0; continue
if (a%2 == b%2):
ans = 2 * max(a, b)
else:
if a > b:
ans = 2 * a + 1
else:
ans = 2 * b - 1
print ans
But wait, double-check when a and b have opposite parity and a == b? That can't happen because parity would be same. So safe.
Now, confirm with given examples: (3,3) a=3,b=3 same parity, max=3, ans=6. (3,4) a=3,b=4 opposite, a<b, ans=2*4-1=7. Good.
Now, edge: (0,0) -> 0.
Now, let's test a scenario: (1000000000, 999999999) we already did.
Now, confirm with some random brute mental for bigger: (10,7): a=10 even, b=7 odd opposite, a>b -> ans=2*10+1=21. Manhattan=17. Possibly minimal? Check n=19? n=19 odd: V=10,H=9; V parity 10 even vs b=7 odd fails. n=21 odd: V=11,H=10; V parity 11 odd vs b=7 odd? 7 odd, 11 odd matches? Wait 11 odd, 7 odd match, yes. H parity 10 even vs a=10 even matches. So 21 works. n=20 even: same parity required? a even, b odd not same, fail. So 21 is minimal. Could n=23 be smaller? No, 21 is smaller. So fine.
Now, (7,10): a=7 odd, b=10 even opposite, a<b -> ans=2*10-1=19. n=19: V=10,H=9; V parity 10 even vs b=10 even ok; H parity 9 odd vs a=7 odd ok. n=17: V=9,H=8; V parity 9 odd vs b=10 even fail. So 19 minimal.
Now, confirm that for a=0,b=2 (same parity), ans=4, which we already checked.
Now, consider negative coordinates: (-3, 4) a=3,b=4 opposite, a<b -> ans=7, matches.
Now, ensure that we didn't forget about the possibility of n=0 for (0,0). Works.
Now, what about (0,0) being output as 0; problem probably expects that.
Now, let's also consider if there is any scenario where a or b is extremely large, the answer fits in int.
Now, the constraints T up to 1e5, so we need O(1) per test.
Now, we should also think about potential overflow when computing 2 * max(a,b) if using 32-bit? max up to 1e9, 2e9 fits in signed 32-bit (2,147,483,647). So safe. However 2*a+1 up to 2e9+1 = 2,000,000,001, also fits. So int is fine. But to be safe, use long long.
Now, final answer.
But before finalizing, let's derive from a more intuitive perspective to ensure no missing trick.
Another way: Since steps alternate, we can think of pairing moves. After two moves (vertical+horizontal), you can end at any point (dx, dy) where |dx| = 1, |dy| = 1. After 4 moves, you can end at (dx,dy) where |dx|≤2, |dy|≤2 with some parity constraints. But our formula captures it.
Now, we might ask: Could the robot ever make a move that does not change direction? No.
Thus solution stands.
Now, let's also verify with some more test cases manually using the conditions.
Case: (a,b) = (4,4) -> same parity -> ans=8. Check: n=8 even: V=4,H=4, ok. n=7 odd: V=4,H=3; H=3 < a=4? Actually H=3 < 4 fails; so 8 minimal.
Case: (5,5) -> 10.
Case: (6,0) -> a=6 even, b=0 even -> ans=12. n=12 even: V=6,H=6; H even matches a even; V even matches b even. n=10 even: V=5,H=5; b=0<=5 ok but V parity 5 odd vs b=0 even fail? Actually V parity must match b parity: V=5 odd, b=0 even fails. Also H=5 odd vs a=6 even fails. So 12 minimal. Could there be n=11 odd? V=6,H=5; V parity 6 even vs b=0 even ok; H parity 5 odd vs a=6 even fails. So 12 minimal.
Case: (7,0) -> a odd, b even? Actually b=0 even, a=7 odd -> opposite parity, a>b -> ans=2*7+1=15. n=15 odd: V=8,H=7; V parity 8 even vs b=0 even ok; H parity 7 odd vs a=7 odd ok. n=13 odd: V=7,H=6; V parity 7 odd vs b=0 even fails. n=14 even: same parity? a odd, b even not same, fail. So 15 minimal.
Now, one more: (0,7) -> a=0 even, b=7 odd opposite, a<b -> ans=2*7-1=13. n=13 odd: V=7,H=6; V parity 7 odd vs b=7 odd ok; H parity 6 even vs a=0 even ok. n=11 odd: V=6,H=5; V parity 6 even vs b=7 odd fails. So 13 minimal.
All good.
Thus solution ready.
Now, we should write code accordingly.
We need to handle input and output efficiently for up to 1e5 cases.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
long long x, y;
cin >> x >> y;
x = abs(x);
y = abs(y);
if (x == 0 && y == 0) {
cout << "0\n";
continue;
}
if ((x % 2) == (y % 2)) {
cout << 2 * max(x, y) << "\n";
} else {
if (x > y) {
cout << 2 * x + 1 << "\n";
} else {
cout << 2 * y - 1 << "\n";
}
}
}
return 0;
}
But careful: x and y are up to 1e9, but we take abs which is fine.
Edge: what if x or y negative? abs fine.
Now, double-check parity: For negative numbers, modulus in C++ can be negative but we take abs first, so fine.
Alternative: use &1 on positive numbers.
Now, confirm that for (0,0) we output 0.
Now, test with given input.
Now, think about potential pitfalls: The problem may have large T and require fast I/O. Using endl might be slow; use "\n". Using ios::sync_with_stdio and cin.tie should be fine.
Now, maybe there is a more compact formula: ans = (a + b) + if (a%2 == b%2) ? (max(a,b) - min(a,b)) : something? Actually we can derive: For same parity, ans = a+b + (max - min) because 2*max = (a+b) + (max-min). For opposite parity, if a<b: ans = 2b-1 = (a+b) + (b - a - 1). If a>b: ans = 2a+1 = (a+b) + (a - b + 1). So ans = D + extra, where extra depends. But not needed.
Now, final answer.
We should also ensure that the answer fits in 64-bit.
Now, I think it's correct.
But wait, let's do a more thorough theoretical check: Our condition for n being feasible is: there exist non-negative integers p,q such that V = p+q, b = p - q, so p = (V+b)/2 integer => V+b even and V >= b. Similarly, H >= a and H and a same parity. And V and H differ by at most 1. So we derived minimal n.
We can also consider that n must satisfy: n >= a+b, but also n has same parity as (a+b) modulo something? Actually not exactly.
Alternate approach: For given a,b, define d = max(a,b). If a and b same parity: answer = 2d. If not: answer = 2d + (d%2 == a%2 ? 1 : -1)? Wait check: For a > b, d = a, answer = 2a+1 = 2d+1. For a < b, d = b, answer = 2b-1 = 2d-1. So answer = 2d + (if d == max and if a > b then +1 else -1). But that's just restating.
But note that in the case a < b, d = b, and we have answer = 2d-1, which is 2d - 1. In the case a > b, answer = 2d+1. So answer = 2d + sign where sign = +1 if the larger coordinate is horizontal (x) and -1 if larger is vertical (y). Since we start vertical, it's easier when vertical is larger.
Thus final.
Now, I'll produce a final answer.
But before concluding, let's consider if there is any other nuance: The robot starts facing the X-axis. Could it possibly turn left or right multiple times before stepping? The description says each move is a turn followed by a step. So no.
Now, I'm confident.
Now, produce final solution code.
Now, we should also consider if input might include trailing spaces, but cin handles.
Now, final answer in required format: just output each answer on a new line.
Now, I'll write the solution with proper comments.
Potential improvement: Pre-compute nothing.
Now, I'll write final answer. ```cpp
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int T;
cin >> T;
while (T--) {
long long x, y;
cin >> x >> y;
x = abs(x);
y = abs(y);
if (x == 0 && y == 0) {
cout << "0\n";
continue;
}
// same parity
if ((x & 1) == (y & 1)) {
cout << 2 * max(x, y) << "\n";
} else {
// opposite parity
if (x > y) {
cout << 2 * x + 1 << "\n";
} else {
cout << 2 * y - 1 << "\n";
}
}
}
return 0;
}