Pizza Orders

Time limit: 0.7s Memory limit: 128MB Input: Output:

A restaurant has NN available ingredients for making pizzas. The ingredients are numbered from 00 to N1N-1. The menu features MM different pizzas, each with a specific list of ingredients.

For any given pizza, the restaurant can:

  • Add ingredient ii for a cost of AiA_i coins;
  • Remove ingredient ii for a cost of BiB_i coins.

You are given QQ queries. In each query, determine the minimum cost required to modify any existing pizza into a specific target pizza.

Input data

The first line of the input consists of three space-separated integers NN, MM, and QQ, denoting the number of ingredients, the number of pizzas, and the number of queries, respectively.

The next NN lines describe the cost of modifying each ingredient. Each line consists of two space-separated integers AiA_i and BiB_i (0iN10 \le i \le N - 1).

The following 2M2 \cdot M lines describe the pizzas on the menu. Each pizza is represented by two consecutive lines in the following form:

  • KK -- the number of ingredients.
  • P0,P1,,PK1P_0, P_1, \dots, P_{K-1} -- the numbers representing each ingredient.

The following 2Q2 \cdot Q lines contain the description of the queries, given in the same format as the description of the pizzas.

Output data

Output QQ lines, the jj-th of which should contain a single integer denoting the answer to the jj-th query.

Constraints and clarifications

  • 1N201 \le N \le 20.
  • 1M,Qmin(200000, 2N)1 \le M, Q \le \min(200\,000,\ 2^N).
  • 0Ai,Bi1090 \le A_i, B_i \le 10^9 for each i=0N1i=0\ldots N-1.
  • 1KN1 \le K \le N.
  • 0PiN10 \le P_i \le N - 1 for each i=0K1i=0\ldots K-1.
  • Pi1<PiP_{i-1} < P_{i} for each i=1K1i=1\ldots K-1.
  • All the MM pizzas are different.
# Points Constraints
1 0 Examples
2 15 M,Qmin(1 000, 2N)M, Q \le \min(1 \ 000,\ 2^N).
3 25 N16N \le 16.
4 60 No additional constraints.

Example

stdin

3 2 3
5 2
3 3
0 10
2
0 1
1
2
1
0
2
0 1
3
0 1 2

stdout

3
0
0

Explanation

In the sample case, there are 33 types of ingredients. The cost of adding each of them is 55, 33, and 00 coins, and the cost of removing each of them is 22, 33, and 1010 coins, respectively.

There are 22 different pizzas on the menu. The first one has two ingredients: ingredient 00 and ingredient 11; and the second pizza has only one ingredient: ingredient 22.

You are asked to create 33 pizzas:

  • In the first query, you have to pay 33 coins to transform one of the two pizzas at the restaurant into a pizza with only ingredient 00.
  • In the second query, the specified pizza already exists, so you don't have to pay anything.
  • In the third query, you can pay 00 coins and add the third ingredient to the first pizza, obtaining the requested pizza.

Log in or sign up to be able to send submissions!