It is the hard version of the problem. The only difference is that in this version 1≤n≤3001≤n≤300.

In the cinema seats can be represented as the table with nn rows and mm columns. The rows are numbered with integers from 11 to nn. The seats in each row are numbered with consecutive integers from left to right: in the kk-th row from m(k−1)+1m(k−1)+1 to mkmk for all rows 1≤k≤n1≤k≤n. Seating Arrangements (hard version) solution codeforces

11 | 22 | ⋯⋯ | m−1m−1 | mm |

m+1m+1 | m+2m+2 | ⋯⋯ | 2m−12m−1 | 2m2m |

2m+12m+1 | 2m+22m+2 | ⋯⋯ | 3m−13m−1 | 3m3m |

⋮⋮ | ⋮⋮ | ⋱⋱ | ⋮⋮ | ⋮⋮ |

m(n−1)+1m(n−1)+1 | m(n−1)+2m(n−1)+2 | ⋯⋯ | nm−1nm−1 | nmnm |

The table with seats indices

It is known, that in this cinema as lower seat index you have as better you can see everything happening on the screen. ii-th person has the level of sight aiai. Let’s define sisi as the seat index, that will be given to ii-th person. You want to give better places for people with lower sight levels, so for any two people ii, jj such that ai<ajai<aj it should be satisfied that si<sjsi<sj.

After you will give seats to all people they will start coming to their seats. In the order from 11 to nmnm, each person will enter the hall and sit in their seat. To get to their place, the person will go to their seat’s row and start moving from the first seat in this row to theirs from left to right. While moving some places will be free, some will be occupied with people already seated. The inconvenience of the person is equal to the number of occupied seats he or she will go through.

Let’s consider an example: m=5m=5, the person has the seat 44 in the first row, the seats 11, 33, 55 in the first row are already occupied, the seats 22 and 44 are free. The inconvenience of this person will be 22, because he will go through occupied seats 11 and 33.

Find the minimal total inconvenience (the sum of inconveniences of all people), that is possible to have by giving places for all people (all conditions should be satisfied).

Seating Arrangements (hard version) solution codeforces

The input consists of multiple test cases. The first line contains a single integer tt (1≤t≤1001≤t≤100) — the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers nn and mm (1≤n,m≤3001≤n,m≤300) — the number of rows and places in each row respectively.

The second line of each test case contains n⋅mn⋅m integers a1,a2,…,an⋅ma1,a2,…,an⋅m (1≤ai≤1091≤ai≤109), where aiai is the sight level of ii-th person.

It’s guaranteed that the sum of n⋅mn⋅m over all test cases does not exceed 105105.

Seating Arrangements (hard version) solution codeforces

For each test case print a single integer — the minimal total inconvenience that can be achieved.

Note

In the first test case, there is a single way to give seats: the first person sits in the first place and the second person — in the second. The total inconvenience is 11.

In the second test case the optimal seating looks like this: