| #include <iostream>
|
| #include <map>
|
| #include <utility>
|
| using namespace std;
|
|
|
| int N, M;
|
|
|
| int solve() {
|
| map<int, pair<int, int>> curr, next;
|
| cin >> N >> M;
|
| for (int i = 0, s; i < M; i++) {
|
| cin >> s;
|
| int v = curr[s].first + 1;
|
| curr[s] = make_pair(v, v);
|
| }
|
| int ans = 0;
|
| for (int i = 0, p; i < N; i++) {
|
| map<int, int> req;
|
| for (int j = 0; j < M; j++) {
|
| cin >> p;
|
| req[p]++;
|
| }
|
| next.clear();
|
| for (auto [k, v] : curr) {
|
| auto it = req.find(k);
|
| int num_to_change = max(0, v.first - (it != req.end() ? it->second : 0));
|
| int num_to_change_for_free = min(num_to_change, v.second);
|
| ans += num_to_change - num_to_change_for_free;
|
| curr[k].second -= num_to_change_for_free;
|
| }
|
| for (auto [k, v] : req) {
|
| auto it = curr.find(k);
|
| next[k] = make_pair(v, min(v, it != curr.end() ? it->second.second : 0));
|
| }
|
| curr = next;
|
| }
|
| return ans;
|
| }
|
|
|
| int main() {
|
| int T;
|
| cin >> T;
|
| for (int t = 1; t <= T; t++) {
|
| cout << "Case #" << t << ": " << solve() << endl;
|
| }
|
| return 0;
|
| }
|
|
|