Waif Until Dark

Match children to toys they like, with a per-category cap on how many toys of each category can be used, and maximize satisfied children.

Medium7GraphBFSGreedyImplementationNo attempts yetTime limit2sMemory limit512 MB

Problem

Waif Until Dark is a daycare center for households where both parents work during the day. To keep the children busy, the center has a set of toys they can play with. Some toys belong to one category, such as sports toys, musical toys, or dolls. To limit wear on those toys, the teachers allow only a fixed number of toys from each category during playtime. Children do not all like the same toys, so handing every child a toy they like is not always possible.

A child receives at most one toy, and a toy goes to at most one child. A child is satisfied when the child receives a toy from their own list. Find the largest number of children that can be satisfied at the same time.

Input

The first line contains the number of children nn, the number of toys mm, and the number of toy categories pp (1n,m1001 \le n, m \le 100, 0pm0 \le p \le m). Children and toys are numbered starting at 1.

Each of the next nn lines has the form k i1 i2  ikk\ i_1\ i_2\ \dots\ i_k (1km1 \le k \le m, 1i1,i2,,ikm1 \le i_1, i_2, \dots, i_k \le m). The jj th of these lines means that child jj is willing to play with toys i1i_1 through iki_k.

Each of the next pp lines has the form l t1 t2  tl rl\ t_1\ t_2\ \dots\ t_l\ r (1rlm1 \le r \le l \le m, 1t1,t2,,tlm1 \le t_1, t_2, \dots, t_l \le m). The jj th of these lines means that toys t1t_1 through tlt_l belong to category jj and that at most rr toys of that category can be used.

A toy belongs to at most one category. A toy that never appears on those pp lines has no category, and all such toys can be used with no limit. No toy number appears twice on the same line.

Output

Print the largest number of children that can be satisfied with a toy they like.