Codeforces Round 1002 (Div. 2)
E2
Stop Gaming (Hard Version)
Codeforces
2900
原题
← E1→
翻译中…

This is the hard version of the problem. The difference between the versions is that in this version you need to output all the operations that need to be performed. You can hack only if you solved all versions of this problem.

翻译中…

You are given $n$ arrays, each of which has a length of $m$. Let the $j$-th element of the $i$-th array be denoted as $a_{i, j}$. It is guaranteed that all $a_{i, j}$ are pairwise distinct. In one operation, you can do the following:

翻译中…
  • Choose some integer $i$ ($1 \le i \le n$) and an integer $x$ ($1 \le x \le 2 \cdot n \cdot m$).
  • For all integers $k$ from $i$ to $n$ in increasing order, do the following:
  • Add the element $x$ to the beginning of the $k$-th array.
    • Assign $x$ the value of the last element in the $k$-th array.
    • Remove the last element from the $k$-th array.
翻译中…

In other words, you can insert an element at the beginning of any array, after which all elements in this and all following arrays are shifted by one to the right. The last element of the last array is removed.

翻译中…

You are also given a description of the arrays that need to be obtained after all operations. That is, after performing the operations, the $j$-th element of the $i$-th array should be equal to $b_{i, j}$. It is guaranteed that all $b_{i, j}$ are pairwise distinct.

翻译中…

Determine the minimum number of operations that need to be performed to obtain the desired arrays, and also output the sequence of all operations itself.

翻译中…

Input

翻译中…

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^4$) — the number of test cases. The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 3 \cdot 10^5$) — the number of arrays and the number of elements in each array.

翻译中…

The $i$-th of the following $n$ lines contains $m$ integers $a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}$ ($1 \le a_{i, j} \le 2 \cdot n \cdot m$) — the elements of the $i$-th original array. It is guaranteed that all $a_{i, j}$ are pairwise distinct.

翻译中…

The $i$-th of the following $n$ lines contains $m$ integers $b_{i, 1}, b_{i, 2}, \ldots, b_{i, m}$ ($1 \le b_{i, j} \le 2 \cdot n \cdot m$) — the elements of the $i$-th final array. It is guaranteed that all $b_{i, j}$ are pairwise distinct.

翻译中…

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

翻译中…

Output

翻译中…

For each test case, output a single integer — the minimum number of operations that need to be performed.

翻译中…

Next, for each operation output two integers $i$ and $x$ ($1 \le i \le n$, $1 \le x \le 2 \cdot n \cdot m$) — the index of the array where the element is inserted and the value of the element, respectively.

翻译中…

If there are multiple possible sequences with the minimum number of operations, output any of them.

翻译中…

Note

翻译中…

In the first test case, the following sequence of $3$ operations is suitable:

翻译中…
  • Apply the operation to the first array with $x = 1$. Then the element $1$ will be added to the beginning of the first array, and the value of $x$ will become $6$. The last element will be removed, and the first array will look like $[1, 2]$. Next, the element $x$ is added to the beginning of the second array, and the value of $x$ becomes $4$. The last element of the second array is removed, and both arrays look like $[1, 2]$ and $[6, 3]$ respectively after the first operation.
  • Apply the operation to the second array with $x = 8$. Then the first array remains unchanged, and both arrays will look like $[1, 2]$ and $[8, 6]$ respectively.
  • Apply the operation to the second array with $x = 7$, then both arrays will have the required appearance $[1, 2]$ and $[7, 8]$ respectively.
翻译中…

In the second test case, the desired array can only be achieved in $5$ operations.

翻译中…

In the third test case, the following sequence of $3$ operations is suitable:

翻译中…
  • Apply the operation with $x = 11$ to the first array.
  • Apply the operation with $x = 12$ to the second array.
  • Apply the operation with $x = 13$ to the third array.
翻译中…

样例

Input #1
4
2 2
2 6
3 4
1 2
7 8
1 5
5 4 1 2 3
5 4 3 2 1
3 3
1 2 3
4 5 6
7 8 9
11 1 2
12 3 4
13 5 6
4 4
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 1 2 3
4 18 5 6
7 19 8 20
9 21 22 10
Output #1
3
1 1
2 8
2 7
5
1 1
1 2
1 3
1 4
1 5
3
1 11
2 12
3 13
6
3 20
2 18
3 19
4 22
4 21
1 17
InputCopy OutputCopy
题目信息
题号
2059E2
时间限制
3 s
内存限制
256 MB
标签
暴力
构造
数据结构
哈希
字符串

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。