#K13051. Minimum Employee Transfers

    ID: 23826 Type: Default 1000ms 256MiB

Minimum Employee Transfers

Minimum Employee Transfers

You are given several test cases. In each test case, there are several companies, each with a list of employee identifiers, and a number of proposed teams, each consisting of a subset of these employees. In order to have each team composed of employees from a single company, you may need to transfer employees. For each team, you can choose one company as the target and transfer the employees who do not already belong to that company. The goal is to minimize the total number of transfers required across all teams in the test case.

Formally, suppose team T has n members. For each company, count the number of members in T that originally belong to that company. Let \( m = \max_{\text{company}}(\text{count}) \). Then the number of transfers required for the team is \( n - m \). The answer for a test case is the sum of transfers for all teams.

Your task is to process the input data and compute the minimum number of employee transfers required for each test case.

inputFormat

The first line contains an integer \( T \) representing the number of test cases. For each test case:

  • The first line contains an integer \( C \) denoting the number of companies.
  • The next \( C \) lines each contain space-separated strings representing the employee IDs belonging to that company.
  • The following line contains an integer \( L \) representing the number of proposed teams.
  • The next \( L \) lines each contain space-separated strings representing the employee IDs in that team.

All input is read from standard input (stdin).

outputFormat

For each test case, output a single integer on a separate line indicating the minimum number of transfers required. The output is printed to standard output (stdout).

## sample
4
2
1_1 1_2
2_1 2_2
2
1_1 2_1
1_2 2_2
3
1_1 1_2
2_1
3_1 3_2
1
1_1 2_1 3_1
2
1_1 1_2
2_1 2_2
2
1_1 1_2
2_1 2_2
3
1_1 1_2
2_1
3_1 3_2
1
1_1 1_2 1_3
2

2 0 0

</p>