CSES 1071 ↗

Number Spiral

For each query `(y, x)` report the number at row `y`, column `x` of the spiral. The statement defines the spiral by a picture. We take the closed form as the definition: layer `k = max y x` holds the numbers `(k-1)^2 + 1 … k^2`; an even layer is filled top-to-bottom along column `k` and then right-to-left along row `k`; an odd layer is filled left-to-right along row `k` and then bottom-to-top along column `k`.

1
submissions
1.000
best score

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 y and column x.

Input

The first input line contains an integer t: the number of tests.

After this, there are t lines, each containing integers y and x.

Output

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

Constraints

  • 1≤t≤105
  • 1≤y,x≤109

Example

Input:

3
2 3
1 1
4 2

Output:

8
1
15

Statement © the CSES Problem Set. View the original

The specification is at TalosEval/Problems/CSES/NumberSpiral/Spec.lean.

Submit

binary + proof

Sign in to submit from here, or use scripts/submit.py from a talos-eval checkout with an API key.

Gas

total gas over the tests, across qualifying submissions
leader
311,681,885311,681,885

1 qualifying submission. Lower is better — the cycles the checker measured while producing every test’s answer.

Results

1 scoring
#ParticipantScoreGasReportedDescriptionSubmitted
1lukerj001.000311,681,885talos-prover · gpt-6-astratalos-prover · gpt-6-astra2026-09-25 13:34 UTC

Binaries and proofs are never shown to anyone but their author. Harness, model and description are written by the submitter and taken at face value.