CF2056A.Shape Perimeter

传统题 时间 2000 ms 内存 256 MiB 3 尝试 1 已通过 1 标签

Shape Perimeter

There is an mm by mm square stamp on an infinite piece of paper. Initially, the bottom-left corner of the square stamp is aligned with the bottom-left corner of the paper. You are given two integer sequences xx and yy, each of length nn. For each step ii from 11 to nn, the following happens:

  • Move the stamp xix_i units to the right and yiy_i units upwards.
  • Press the stamp onto the paper, leaving an mm by mm colored square at its current position.

Note that the elements of sequences xx and yy have a special constraint: 1xi,yim11\le x_i, y_i\le m - 1.

Note that you do not press the stamp at the bottom-left corner of the paper. Refer to the notes section for better understanding.

It can be proven that after all the operations, the colored shape on the paper formed by the stamp is a single connected region. Find the perimeter of this colored shape.

Input

Each test contains multiple test cases. The first line contains the number of test cases tt (1t10001 \le t \le 1000). The description of the test cases follows.

The first line of each test case contains two integers nn and mm (1n1001 \le n \le 100, 2m1002 \le m \le 100) — the number of operations performed and the side length of the square stamp.

The ii-th of the next nn lines contains two integers xix_i and yiy_i (1xi,yim11 \le x_i, y_i \le m - 1) — the distance that the stamp will be moved right and up during the ii-th operation, respectively.

Note that there are no constraints on the sum of nn over all test cases.

Output

For each test case, output a single integer representing the perimeter of the colored shape on the paper.

Note

In the first example, the stamp has a side length of 33 and is pressed 44 times at coordinates (1,1)(1, 1), (3,3)(3, 3), (5,4)(5, 4), and (6,6)(6, 6). The piece of paper looks like that afterwards:

Here, the square formed by the first press is colored blue, the second red, the third green, and the fourth purple. The combined shape, whose perimeter we need to calculate, looks like that:

From the diagram, it can be seen that this shape has a perimeter of 3232.

Samples

3
4 3
1 1
2 2
2 1
1 2
1 2
1 1
6 7
3 6
1 1
3 1
6 6
5 4
6 1
32
8
96

在线编程 IDE

建议全屏模式获得最佳体验