Codeforces Round 1002 (Div. 2) |
---|
Finished |
This is the easy version of the problem. The difference between the versions is that in this version you only need to find the minimum number of operations. 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:
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.
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$$$.
For each test case, output a single integer — the minimum number of operations that need to be performed.
42 22 63 41 27 81 55 4 1 2 35 4 3 2 13 31 2 34 5 67 8 911 1 212 3 413 5 64 41 2 3 45 6 7 89 10 11 1213 14 15 1617 1 2 34 18 5 67 19 8 209 21 22 10
3 5 3 6
In the first test case, the following sequence of $$$3$$$ operations is suitable:
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:
Name |
---|