Under construction

Still being built. Things may change

Team Division

Time limit1sMemory limit256 MB

Summary
Split N employees into attack and defense teams to maximize total power minus a penalty for each pair of task-force members placed on different teams.
Level

Medium7 of 10

Topics
Graph, BFS
Solved
No attempts yet

Problem

A mock internal security hackathon will be held soon. The N participating employees will be split into an attack team (hacking) and a defense team (security). Every employee's attack power and defense power were quantified from the company's previous internal hacking practice contest and stored in a database. Let the attack power of employee ii be AiA_i and the defense power be BiB_i.

There are 2N2^N ways to split the N employees into the two teams. We want the most suitable split. The fitness score is computed as follows. First, add the sum of attack power of the employees in the attack team and the sum of defense power of the employees in the defense team. Then subtract the penalties described below.

The company currently has M task forces. Each task force contains between 2 and N employees. If two employees in the same task force are assigned to different teams, the fitness score is reduced by that task force's penalty weight. The penalty is applied separately to every pair of employees in the task force.

For example, let N = 3 and the employees have the following attack and defense power.

  • Employee 1: attack 10, defense 0
  • Employee 2: attack 10, defense 5
  • Employee 3: attack 5, defense 5

Suppose there is one task force containing employees 1 and 2, with penalty weight 100. If the attack team is {1, 2} and the defense team is {3}, the attack sum is 20, the defense sum is 5, and the penalty is 0, so the fitness is 25. If the attack team is {1} and the defense team is {2, 3}, the attack sum is 10, the defense sum is 10, and the penalty is 100, so the fitness is -80. If the attack team is {1, 2, 3} and the defense team is empty, the attack sum is 25, the defense sum is 0, and the penalty is 0, so the fitness is 25. The maximum fitness here is 25, and the two splits above achieve it. Assigning all employees to the attack team or all of them to the defense team is also allowed. The security staff are assigned to the teams separately, so they are not part of this calculation.

Input

The first line contains the number of test cases TT.

The first line of each test case contains NN and MM, separated by a space.

The next NN lines each contain the attack power and defense power of one employee, separated by a space.

The next 2M2M lines describe the MM task forces, two lines each. The first line of task force ii contains KiK_i, the number of employees in the task force, and SiS_i, the penalty weight, separated by a space. The penalty weight SiS_i is the penalty applied to each pair of employees in task force ii who are assigned to different teams. The second line contains the KiK_i employee numbers in task force ii, separated by spaces. The employee numbers within one task force are all distinct.

Output

For each test case, print two lines.

On the first line, print the maximum fitness score that can be achieved.

On the second line, print the attack team. The first number is the size of the attack team, followed by the employee numbers in the attack team, separated by spaces.

If several splits achieve the maximum fitness, print any of them. The employee numbers may be printed in any order.

Constraints

1≤T≤101 \le T \le 10

0≤Ai,Bi≤1,000,0000 \le A_i, B_i \le 1{,}000{,}000

2≤Ki≤N2 \le K_i \le N

1≤Si≤1,0001 \le S_i \le 1{,}000

An employee may belong to several task forces.

Examples1

  1. Example 1

    Input
    4
    3 1
    10 0
    10 0
    5 5
    2 100
    1 2
    4 2
    10 0
    20 10
    5 10
    1 5
    2 2
    1 2
    3 1
    2 3 4
    4 2
    100 0
    200 10
    50 10
    10 5
    2 2
    1 2
    3 1
    2 3 4
    4 1
    100 0
    4 8
    2 15
    3 51
    4 3
    1 2 3 4
    
    Expected output
    25
    2 1 2
    43
    2 1 2
    360
    4 1 2 3 4
    165
    1 1