← Home
write a go solution for Description:
Alchemist Nastya loves mixing potions. There are a total of n types of potions, and one potion of type i can be bought for c_i coins.

Any kind of potions can be obtained in no more than one way, by mixing from several others. The potions used in the mixing process will be consumed. Moreover, no potion can be obtained from itself through one or more mixing processes.

As an experienced alchemist, Nastya has an unlimited supply of k types of potions p_1,p_2,...,p_k, but she doesn't know which one she wants to obtain next. To decide, she asks you to find, for each 1<=i<=n, the minimum number of coins she needs to spend to obtain a potion of type i next.

Input Format:
The first line of each test contains an integer t (1<=t<=10^4) — the number of test cases.

Each test case is described as follows:

The first line contains two integers n and k (1<=k<n<=2*10^5) — the total number of potion types and the number of potion types Nastya already has.

The second line contains n integers c_1,c_2,...,c_n (1<=c_i<=10^9) — the costs of buying the potions.

The third line contains k distinct integers p_1,p_2,...,p_k (1<=p_i<=n) — the indices of potions Nastya already has an unlimited supply of.

This is followed by n lines describing ways to obtain potions by mixing.

Each line starts with the integer m_i (0<=m_i<n) — the number of potions required to mix a potion of the type i (1<=i<=n).

Then line contains m_i distinct integers e_1,e_2,...,e_m_i (1<=e_j<=n, e_jnei) — the indices of potions needed to mix a potion of the type i. If this list is empty, then a potion of the type i can only be bought.

It is guaranteed that no potion can be obtained from itself through one or more mixing processes.

It is guaranteed that the sum of all n values across all test cases does not exceed 2*10^5. Similarly, it is guaranteed that the sum of all m_i values across all test cases does not exceed 2*10^5.

Output Format:
For each test case, output n integers — the minimum number of coins Nastya needs to spend to obtain a potion of each type.

Note:
In the first test case of the first sample, it is optimal:

- Get a potion of the first type by buying and mixing 2, 4 and 5;
- a potion of the second type can only be obtained by purchasing it;
- Nastya already has an unlimited number of potions of the third type;
- a potion of the fourth type is more profitable to buy than to buy and mix other potions;
- a potion of the fifth type can only be obtained by purchasing it.. Output only the code with no comments, explanation, or additional text.