Game

4.2

9 votes
Dynamic Programming, Algorithms, Approved, Easy
Problem

Consider the following game. There is a field with n rows and 3 columns. Some cells contain obstacles. But the first row is always empty. Let's define cell in i-th row and j-th column as (i,j). From cell (i,j) you can go to cells (i+1,j1), (i+1,j) and (i+1,j+1) if such cell exists (you can't go out of field) and doesn't contain obstacle. You can start in any column in the first row. What is the maximum i, such that i-th row is reachable?

Input format

Given several test cases.

First line of input contains integer T (1T3105) — number of test cases.

Then follow T blocks of input.

The first line of block contains integers n and q (2n109, 0qmin(3105,3(n1))) — number of rows and number of obstacles, respectively.

Then follow q lines with obstacles description.

The i-th of them contains integers x and y (2xn, 1y3) — the coordinates of i-th obstacle, located at (x,y).

Coordinates of all obstacles in a single test case are pairwise distinct.

Sum of all q in input doesn't exceed 3105.

Output format

For each block print single integer — the maximum row that is possible to be reached.

Sample Input
2
4 3
2 2
2 3
4 1
2 3
2 1
2 2
2 3
Sample Output
4
1
Time Limit: 1
Memory Limit: 512
Source Limit:
Explanation

In the first test case one of the optimal paths is (1,2), (2,1), (3,2), (4,2). So the row 4 is possible to be reached.

In the second test case there are no possible moves from (1,2). So only the row 1 is possible to be reached.

Editor Image

?