402919: GYM100947 G Square Spiral Search
Description
A new computer scientist is trying to develop a new memory management system called "spiral memory management".
The basic idea of this system is that it represents the memory as a 2D grid with a predefined origin point, and the address of each memory location is represented as its (x,y) coordinates, and it tries to store frequently used data as close to the origin point as possible.
This system needs a search algorithm. Our computer scientist is currently developing an algorithm called "square spiral search".
The algorithm searches the memory locations in the following order (also shown in the figure):
(0,0), (1,0), (1,1), (0,1), (-1,1), (-1,0), (-1,-1), (0,-1), (1,-1), (2,-1), (2,0), (2,1), (2,2), (1,2), (0,2), (-1,2), (-2,2), (-2,1), (-2,0), (-2,-1), (-2,-2,) ,(-1,-2) ... and so on.
Now he is wondering: how many steps would it take to reach a memory location with the address (x, y) using the square spiral search. Can you help him find the answer?
InputThe input starts with T the number of test cases, T test cases follows.
Each test case consists of two numbers: X, Y the address of the memory location.
- 1, 000, 000, 000 ≤ X ≤ 1, 000, 000, 000
- 1, 000, 000, 000 ≤ Y ≤ 1, 000, 000, 000
OutputFor each test case print the number of steps it would take to reach the memory location ( x, y ) .
ExamplesInput3Output
1 0
1 1
-2 1
1Note
2
17
The number of steps to reach a memory location is defined as the number of memory locations searched before searching the requested location.