Reachable Sequences

아직 제출이 없습니다시간 제한4초메모리 제한512 MB

문제

ZZX has a sequence aa, which is a permutation of 1,2,,n1, 2, \ldots, n. Now ZZX wants to perform some modifications on this sequence. For each modification, he can choose a pair of integers ii and jj, satisfying 1i<jn1 \le i < j \le n and a_i>a_ja\_i > a\_j, and then swap a_ia\_i and a_ja\_j.

If a permutation bb can be obtained by performing some (possibly zero) modifications on the initial sequence aa, then ZZX says bb is reachable from aa.

Now JRY has mm sequences a(1)a^{(1)}, a(2)a^{(2)}, \ldots, a(m)a^{(m)}. Each of them is a permutation of 1,2,,n1, 2, \ldots, n. He wants to know how many pairs (i,j)(i, j) such that 1im1 \le i \le m and 1jm1 \le j \le m have the property that a_ia\_i is reachable from a_ja\_j.

입력

The first line contains an integer TT. Then TT test cases follow. In each test case:

The first line contains two integers nn and mm. After that, mm lines follow. The kk-th of them contains nn integers a(k)_1a^{(k)}\_1, a(k)_2a^{(k)}\_2, \ldots, a(k)_na^{(k)}\_n. Each a(k)a^{(k)} is a permutation of 1,2,,n1, 2, \ldots, n.

There are at most 10001000 small test cases and 11 large test case. The small test cases satisfy 1n51 \le n \le 5 and 1m5001 \le m \le 500. The large test case satisfies 1n91 \le n \le 9 and 1m31051 \le m \le 3 \cdot 10^5.

출력

For each test case, print the answer on a separate line.