Number Spiral

Prev: permutations Next: two-knights

A number spiral is an infinite grid whose upper-left square has number 1.
Here are the first five layers of the spiral:

Your task is to find out the number in row and column .

Input

The first input line contains an integer : the number of test cases.
After this, there are lines, each containing two integers and .

Output

For each test case, print the number in row and column .

Constraints


Example

Input:

3
2 3
1 1
4 2

Output:

8
1
15

Answer

Case 1: (we’re below the diagonal)

If is even → the spiral fills bottom to top, so value decreases as increases.

If is odd → spiral fills top to bottom, so value increases as increases.

Case 2: (we’re right of the diagonal)

If is even → spiral fills left to right, increasing in .

If is odd → spiral fills right to left, decreasing in .

#include <iostream>
 
int main() {
    int t;
    std::cin >> t;
    while (t--) {
        long long a, b;
        std::cin >> a >> b;
        long long ans;
 
        if (a > b) {
            if (a % 2 == 0) {
                ans = a * a - b + 1;
			} else {
                ans = (a - 1) * (a - 1) + b;
			}
        } else {
            if (b % 2 == 0) {
                ans = (b - 1) * (b - 1) + a;
			} else {
                ans = b * b - a + 1;
			}
        }
 
        std::cout << ans << '\n';
    }
}

Prev: permutations Next: two-knights