Under construction

Still being built. Things may change

Security Game

Time limit2sMemory limit512 MB

Summary
For each B, find the maximum total protections where each robot covers at most B buildings and each building has between L and U robots, or -1 if impossible.
Level

Medium7 of 10

Topics
Graph, Implementation
Solved
No attempts yet

Problem

Albert enjoys a board game called "Security Game". The goal is to protect MM buildings with NN security robots. The game has the following rules.

  1. Each robot can protect up to BB different buildings at the same time.
  2. Not every robot can protect every building. The ii-th robot can protect gig_i different buildings, denoted xi,jx_{i,j} (1≤j≤gi1 \le j \le g_i, 1≤xi,j≤M1 \le x_{i,j} \le M).
  3. The kk-th building must be protected by at least LkL_k and at most UkU_k different robots. The score of each building is the number of robots guarding it.
  4. If the robots are placed while following all the rules above, the game score is the sum of the building scores. If no placement follows all the rules, the game score is -1.

For convenience, SbS_b is defined as the maximum game score Albert can get when B=bB = b (1≤B≤M1 \le B \le M).

For example, let N=M=3N = M = 3, g=[2,2,2]g = [2, 2, 2], x=[[1,2],[1,3],[2,3]]x = [[1, 2], [1, 3], [2, 3]], L=[1,1,1]L = [1, 1, 1], and U=[3,3,3]U = [3, 3, 3].

  • If B=1B = 1, the maximum score is 3. Robot 1 protects building 2, robot 2 protects building 1, and robot 3 protects building 3. Each building scores 1, so the game score is 3.
  • If B=2B = 2, the maximum score is 6. Robot 1 protects buildings 1 and 2, robot 2 protects buildings 1 and 3, and robot 3 protects buildings 2 and 3. Each building scores 2, so the game score is 6.
  • If B=3B = 3 or more, no placement scores more than 6. Therefore S=[3,6,6]S = [3, 6, 6].

As another example, let N=4N = 4, M=3M = 3, g=[3,1,1,1]g = [3, 1, 1, 1], x=[[1,2,3],[1],[1],[3]]x = [[1, 2, 3], [1], [1], [3]], L=[1,2,1]L = [1, 2, 1], and U=[2,2,2]U = [2, 2, 2].

  • Building 2 has L2=U2=2L_2 = U_2 = 2, so exactly 2 different robots must protect it.
  • Only robot 1 can protect building 2, so the game score is -1 regardless of the value of BB.
  • In this case S=[−1,−1,−1]S = [-1, -1, -1].

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 gig_i, the number of buildings where the ii-th robot can be placed, followed by the gig_i building numbers xi,1,xi,2,…,xi,gix_{i,1}, x_{i,2}, \dots, x_{i,g_i}, separated by spaces. In other words, each of these lines contains gi+1g_i + 1 integers. The next MM lines each contain a pair of integers LkL_k and UkU_k, the minimum and maximum number of robots that must be placed on the kk-th building.

Output

For each test case, print S1,S2,…,SMS_1, S_2, \dots, S_M separated by spaces on a single line.

Constraints

  • 1≤T≤101 \le T \le 10
  • 1≤N,M≤2001 \le N, M \le 200
  • ∑1≤i≤Ngi≤5000\sum_{1 \le i \le N} g_i \le 5000
  • For each ii with 1≤i≤N1 \le i \le N:
    • 1≤gi≤M1 \le g_i \le M
    • 1≤xi,1,xi,2,…,xi,gi≤M1 \le x_{i,1}, x_{i,2}, \dots, x_{i,g_i} \le M
    • xi,1,xi,2,…,xi,gix_{i,1}, x_{i,2}, \dots, x_{i,g_i} contain no duplicate values
  • For each kk with 1≤k≤M1 \le k \le M: 1≤Lk≤Uk≤N1 \le L_k \le U_k \le N

Examples1

  1. Example 1

    Input
    3
    3 3
    2 1 2
    2 1 3
    2 2 3
    1 3
    1 3
    1 3
    4 3
    3 1 2 3
    1 1
    1 1
    1 3
    1 2
    2 2
    1 2
    5 3
    2 1 2
    1 3
    2 1 2
    1 2
    1 3
    1 2
    2 2
    1 1
    
    Expected output
    3 6 6
    -1 -1 -1
    4 5 5