CF2148C.Pacer

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

Pacer

The FitnessGram Pacer Test is a multistage aerobic capacity test that progressively gets more difficult as it continues. The 20 meter pacer test will begin in 30 seconds. Line up at the start. A single lap should be completed every time you hear this sound. Ding! Remember to run in a straight line and run as long as possible. The test will begin on the word start. On your mark. Get ready!...

Farmer John is running the FitnessGram Pacer Test! Farmer John takes one minute to run to the other side of the gym. Therefore, at the start of each minute, FJ can choose to either run to the other side of the gym or stay in place. If he chooses to run to the other side of the gym, he gains one point.

FJ will run the Pacer Test until the start of the mm-th minute. Initially (at the start of the 00-th minute), FJ is at the starting side of the gym, which we will denote as side 00. The opposite side of the gym is denoted side 11.

The pacer test audio plays nn times. At the start of the aia_i-th minute, FJ must be at the bib_i-th side of the gym.

What is the maximum number of points FJ can acquire while ensuring that he meets the audio's requirements?

Input

The first line contains an integer tt (1t1041 \leq t \leq 10^4) — the number of test cases.

The first line of each test case contains two integers nn and mm (1n2105,nm1091 \leq n \leq 2 \cdot 10^5, n \leq m \leq 10^9) — the number of requirements and the number of total minutes.

The following nn lines contain two integers aia_i and bib_i (1aim,bi{0,1})(1 \leq a_i \leq m, b_i \in \{0,1\}) — the ii-th requirement by the audio. It is guaranteed that ai>ai1a_i \gt a_{i-1} over all i>1i \gt 1.

It is guaranteed that the sum of nn over all test cases does not exceed 21052 \cdot 10^5.

Output

For each test case, output the maximum number of points that FJ can acquire.

Note

For the first sample test case,

  • During minute 00, FJ can stay at side 00.
  • During minute 11, FJ can run to side 11 and gain 11 point.
  • Right before minute 22, the audio requires FJ to be at side 11. Here, FJ is indeed at side 11.
  • During minute 22, FJ can run to side 00 and gain 11 point.
  • During minute 33, FJ can stay at side 00.
  • Right before minute 44, the audio requires FJ to be at side 00. Here, FJ is indeed at side 00.
  • Since the start of minute 44 has reached, the Pacer Test ends. His total score is 22.

Relevant illustration of the statement:

Samples

3
2 4
2 1
4 0
2 7
1 1
4 0
4 9
1 0
2 0
6 1
9 0
2
7
6

在线编程 IDE

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