Under construction

Still being built. Things may change

Cyclic Triangle

Time limit5sMemory limit512 MB

Summary
Given a triangle size and cell coordinates, report the digit placed at that cell by the spiral border-filling order.
Level

Medium5 of 10

Topics
Math, Implementation, Simulation
Solved
No attempts yet

Problem

A right triangle grid of size NN is filled with digits. Both legs have length NN. One leg lies horizontally along the top, the other lies vertically along the right side. Columns are numbered 00 at the left end through N−1N-1 at the right end, and rows are numbered 00 at the top through N−1N-1 at the bottom. The right angle is therefore at row 00, column N−1N-1, and the cell at row RR, column CC exists only when R≤CR \le C. The hypotenuse runs from row 00, column 00 to row N−1N-1, column N−1N-1.

The filling walks once around the border of the triangle.

  1. Start at row 00, column 00, the end of the horizontal leg opposite the right angle, and fill rightward along that leg up to row 00, column N−1N-1.
  2. Continue downward along the vertical leg to row N−1N-1, column N−1N-1.
  3. Then move up and to the left along the hypotenuse, filling as far as row 11, column 11.

The cells that are still empty now form a right triangle of size N−3N-3 whose top left corner is row 11, column 22. Apply the same rule to that triangle, and stop once the size drops to 00 or below.

The digits follow the filling order and repeat as 1,2,…,9,0,1,2,…1, 2, \dots, 9, 0, 1, 2, \dots. The kk-th cell filled gets the remainder of kk divided by 1010.

For N=10N = 10 the finished table is shown below. A dot marks a position outside the triangle.

1234567890
.789012341
..65678952
...5445063
....433174
.....32285
......2196
.......107
........08
.........9

Once NN is known, the digit in any cell can be found directly. In the table above, row 00, column 77 holds 88, and row 55, column 77 holds 22. Given a row index and a column index, report the digit written in that cell.

Input

The first line contains the number of test cases TT (1≤T≤101 \le T \le 10).

The first line of each test case contains the triangle size NN and the number of queries QQ, separated by a single space (1≤N≤10000001 \le N \le 1000000, 1≤Q≤101 \le Q \le 10).

Each of the next QQ lines contains a row index RR and a column index CC, separated by a single space (0≤R≤C<N0 \le R \le C < N). Rows and columns are both counted from 00.

Output

For each query print the digit written in the cell at row RR, column CC, one per line, in the order the queries are given.

Examples2

  1. Example 1

    Input
    2
    12 5
    7 8
    0 0
    11 11
    5 10
    2 7
    1000000 6
    7000 8000
    5212 6824
    700000 999999
    5000 72913
    900000 955555
    8775 57412
    
    Expected output
    2
    1
    3
    6
    1
    8
    0
    0
    4
    8
    3
    
  2. Example 2

    Input
    4
    1 1
    0 0
    2 3
    0 0
    0 1
    1 1
    3 6
    0 0
    0 1
    0 2
    1 1
    1 2
    2 2
    4 10
    0 0
    0 1
    0 2
    0 3
    1 1
    1 2
    1 3
    2 2
    2 3
    3 3
    
    Expected output
    1
    1
    2
    3
    1
    2
    3
    6
    4
    5
    1
    2
    3
    4
    9
    0
    5
    8
    6
    7