This page is still under construction.

Parts of this page are still being built. What you see may change.

Obélix

Time limit3sMemory limit512 MB

Summary
Choose a distinct recipe for each of n days so no ingredient is used after its expiration day, maximizing the total grade.
Level

Medium7 of 10

Topics
Greedy, Sorting, Heap, Intervals
Solved
No attempts yet

Problem

Obélix is a big eater. Everybody knows that, even the president of the Bureau des Élèves. He eats wild boar every day. But only a few people know that he is also a fine diner. In fact, Obélix never cooks his meat according to the same recipe twice. He keeps all the recipes he might want to try one day in an old book. Every recipe comes with a list of required ingredients to prepare the dish. He has even rated each recipe, in the form of a grade, according to how good he believes the dish will taste. In his kitchen pantry, he has all the required ingredients in unlimited quantities. The only drawback is that the ingredients will eventually lose their freshness and spoil as all the items of a given ingredient have the same expiration date.

In the coming days, Obélix would like to prepare a different dish every day in order to maximize the total grade of all the dishes he cooks, while never using an expired ingredient. Astérix is fortunate enough to be his guest every day, but they will have a hard time figuring out how to choose which dish to cook. Can you help?

Input

The input consists of multiple test cases. The first line of the input consists of an integer indicating the number of test cases. The first line of each test case consists of three integers nn (1≤n≤1000001\le n \le 100000), ii (1≤i≤1000001\le i \le 100000) and rr (1≤r≤1000001\le r \le 100000) separated by single spaces: nn indicates the number of days during which Obélix will cook (the days are numbered from 11 to nn), ii is the number of ingredients (numbered from 11 to ii), and rr is the number of recipes in the book. The second line consists of ii integers separated by single spaces: for 1≤j≤i1 \leq j \leq i, the jjth integer 1≤e_j≤1000001 \leq e\_j \leq 100000 indicates the day on which the ingredient will expire (i.e., the last day when it can be used). The test cases finish with rr lines, one for each recipe, with the kkth of these lines for 1≤k≤r1 \leq k \leq r consisting of integers separated by single spaces: a first integer 1≤g_k≤1001 \leq g\_k \leq 100 giving the grade of the recipe, a second integer 1≤l_k≤101 \leq l\_k \leq 10 giving the number of ingredients of the recipe, and l_kl\_k distinct integers giving the required ingredients, each of which is between 11 and ii.

Output

For each test case, your program should output the maximum sum of the grades of recipes that Obélix can cook over the nn days, subject to the constraint that each recipe can be cooked at most once and that a recipe cannot be cooked on a day where one of its ingredients is past its expiration date.

Examples1

  1. Example 1

    Input
    2
    2 3 2
    1 2 6
    5 2 2 3
    10 1 1
    3 3 3
    1 2 3
    15 1 1
    5 2 2 3
    10 1 1
    
    Expected output
    15
    20