#P7512. [2017年杭电多校]Loop nest

[2017年杭电多校]Loop nest

Loop nest

Problem Description

There are mm sets Pi,QiP_i,Q_i£¬i(1im),Pi,Qi{1¡­i1}\forall i(1\leq i\leq m),Pi,Qi\subseteq \{1¡­i-1\}. There are nested loops with mm layers, and for the jjth layer, the loop variable is iji_j, the lower bound equals max{ik(kPj)}\max\{i_k(k\in P_j)\}(especially, when PjP_j is empty set, it means the lower bound equals 11), the upper bound equals min{ik(kQj)}\min\{i_k(k\in Q_j)\}(especially, when QjQ_j is empty set, it means the upper bound equals nn). HazelFan want to know how many times the loop body will be executed, module pp.

Input

The first line contains a positive integer T(1T5)T(1\leq T\leq5), denoting the number of test cases. For each test case: The first line contains three positive integers $m,n,p(1\leq m\leq15,1\leq n\leq10^9,2\leq p\leq10^9+7)$. The next mm lines, the iith line contains several nonnegative integers describing the sets Pi,QiP_i,Q_i. The first integer is AiA_i, denoting the size of PiP_i, then next AiA_i integers denotes the elements of PiP_i. The next integer is BiB_i, denoting the size of QiQ_i, then next BiB_i integers denotes the elements of QiQ_i.

Output

For each test case: A single line contains a nonnegative integer, denoting the answer.

Sample Input

2

2 10 233

0 0

1 1 0

6 10 987654321

0 0

1 1 0

0 0

1 3 0

0 1 4

1 2 2 1 2

Sample Output

55

3850

Source

2017 Multi-University Training Contest - Team 7

https://acm.hdu.edu.cn/showproblem.php?pid=6131