Matrix is Fun#13036

Matrix is Fun

1 second
256 megabytes
Easy

You are given an n \times m matrix .

In one move, you may choose any column and cyclically shift it upwards by any number of positions (including performing zero moves overall).

A column is called Perfect if there exists a sequence of moves such that, in the final matrix, every element in column is in its correct position (equivalently, every element in column satisfies \text{col}(x)=j and is placed in row ).

Determine the number of Perfect columns.

For example :

M= \begin{bmatrix} 5 & 2 & 7 & 4\\ 1 & 6 & 3 & 8 \end{bmatrix}.

Column is already Perfect. If we cyclically shift column upwards by , we get

\begin{bmatrix} 1 & 2 & 7 & 4\\ 5 & 6 & 3 & 8 \end{bmatrix},

and then shifting column upwards by gives

\begin{bmatrix} 1 & 2 & 3 & 4\\ 5 & 6 & 7 & 8 \end{bmatrix}.

Now every column is Perfect.

Input

The first line contains a single integer (1 \le t \le 10^4) — the number of test cases.

The first line of each test case contains two integers and (1 \le n\cdot m \le 2\cdot 10^5) — the number of rows and the number of columns, respectively.

The next lines each contain integers M_{i,1}, M_{i,2}, \ldots, M_{i,m} (1 \le M_{i,j} \le n\cdot m). The -th of these lines describes the -th row of the matrix .

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

Output

For each test case, output a single integer — the number of columns that can be made perfect after performing any number of upward cyclic shifts.

Examples

Example 1

Loading
Loading

Note

In the first test case, no operations are needed: every column of the matrix is already Perfect.

In the second test case, it can be shown that no matter which operations you apply, you cannot make even a single column Perfect.

In the third test case, the entire matrix can be made Perfect using the following operations:

  • Cyclically shift column upwards by :

    \begin{bmatrix} \color{blue}{1} & 2 & 7 & 4 \\ \color{blue}{5} & 6 & 3 & 8 \\ \end{bmatrix}
  • Cyclically shift column upwards by :

    \begin{bmatrix} 1 & 2 & \color{blue}{3} & 4 \\ 5 & 6 & \color{blue}{7} & 8 \\ \end{bmatrix}

Now every element is in its correct position, so the whole matrix is Perfect.

Scoring

  1. Subtask ( points) : (1 \le n,m \le 100)

  2. Subtask ( points) : No additional constraints.