P12356 「HCOI-R2」Rabbit Panic (Hard Version)

Background

**Note that in this version of the problem, you need to minimize the number of operations.**

Description

You have a sequence $\{p_n\}$ where initially $p_i = i(1 \leq i \leq n)$. Each time you can choose $m$ **distinct** numbers $i_1, i_2\dots i_m$, and **simultaneously** set all of $p_{i_1}, p_{i_2}\dots p_{i_m}$ into their mean. **Note that the mean may not be an integer.** In this case, it will never be rounded. You need to minimize the number of operations to make $p_1 = p_2 = \dots = p_n$.

Input Format

Each test consists of multiple test cases. The first line contains a single integer $T(1\leq T\leq 1.2\times 10^4)$ --- the number of sets of test cases. The description of each test case follows. In each test case, the only line contains two integers --- $n$($1 \leq n \leq 2\times 10^5$) and $m$($1\le m\le n$), representing the length of the sequence and the number $m$. It's guaranteed that the sum of $n$ over all testcases does not exceed $10^6$.

Output Format

For each test case, on the first line, output the minimum number of operations, $s$. On the next $s$ lines, each line contains $m$ distinct integers in the range $[1, n]$ representing an operation. If there's no solution, just print $s = -1$. If there are multiple solutions, print any.

Explanation/Hint

### Sample #1 - $[1,2,3,4,5,6]\to [3.5,3.5,3,4,3.5,3.5]\to [3.5,3.5,3.5,3.5,3.5,3.5]$。 - There may be different but valid solutions. ### Constraints **This problem uses subtasks.** Note that the full score of this problem in this contest is $50$. Your actually score is half of the subtask scores displayed below. - Subtask 0 (20 pts): $1\leq \sum n\leq 10$. - Subtask 1 (40 pts): $1\leq \sum n\leq 10^3$. - Subtask 2 (40 pts): No Special Constraints. It is guaranteed that $1 \leq T \leq 1.2\times 10^4$, $1 \leq m \leq n \leq 2\times 10^5$, $1 \leq \sum n \leq 10^6$.