H. Subset Query
time limit per test
3 seconds
memory limit per test
512 megabytes
input
standard input
output
standard output

Little Q and Little T are playing a guessing game. In each round, Little T comes up with $$$n$$$ positive integers $$$x_1,x_2,\dots,x_n$$$ and writes down them on the paper. He should ensure these integers are all in the range $$$[1,m]$$$, and he is not allowed to change them during the game. Then Little Q can ask as many questions as he likes. There are two types of questions Little Q can ask:

  • "What's the value of the minimum number in the subset $$$S$$$?"
  • "What's the value of the maximum number in the subset $$$S$$$?"

For each question, Little T will always answer it honestly in the form "It's in the range $$$[l,r]$$$."

Now Little Q has asked $$$q$$$ questions, he still can't figure out the value of $$$x_1,x_2,\dots,x_n$$$, can you please tell him the minimum possible value of $$$x_1+x_2+\dots+x_n$$$ and the maximum possible value of $$$x_1+x_2+\dots+x_n$$$?

Input

The first line contains a single integer $$$T$$$ ($$$1 \leq T \leq 5$$$), the number of test cases. For each test case:

The first line contains three integers $$$n$$$, $$$m$$$ and $$$q$$$ ($$$1 \leq n \leq 8$$$, $$$1\leq m\leq 1\,000\,000$$$, $$$1\leq q\leq 500$$$), denoting the number of numbers, the upper bound of numbers and the number of questions.

Each of the following $$$q$$$ lines starts with two integers $$$op_i$$$ and $$$k_i$$$ ($$$1\leq op_i\leq 2,1\leq k_i\leq n$$$), followed by $$$k_i$$$ distinct integers $$$p_{i,1},p_{i,2},\dots,p_{i,k_i}$$$ ($$$1\leq p_{i,j}\leq n$$$), ending with two integers $$$l_i$$$ and $$$r_i$$$ ($$$1\leq l_i\leq r_i\leq m$$$), describing each question and the answer:

  • $$$op_i=1$$$ means the value of the minimum number among the set $$$\{x_{p_{i,1}},x_{p_{i,2}},\dots,x_{p_{i,k_i}}\}$$$ is in the range $$$[l_i,r_i]$$$.
  • $$$op_i=2$$$ means the value of the maximum number among the set $$$\{x_{p_{i,1}},x_{p_{i,2}},\dots,x_{p_{i,k_i}}\}$$$ is in the range $$$[l_i,r_i]$$$.
Output

For each test case, output a single line containing two integers $$$\min\{sum\}$$$ and $$$\max\{sum\}$$$, denoting the minimum possible value of $$$x_1+x_2+\dots+x_n$$$ and the maximum possible value of $$$x_1+x_2+\dots+x_n$$$.

It is guaranteed that the answer always exists.

Example
Input
2
3 10 1
1 1 1 5 5
5 100 2
1 3 2 3 4 7 9
2 3 2 3 4 7 9
Output
7 25
23 227