# Golden Stone Solutions Round E | Kick Start 2020

Page Contents

### Golden Stone Solutions Round E | Kick Start 2020

Leopold’s companion Kate likes stones, so he chose to give her a brilliant stone as a blessing. There are S kinds of stones numbered from 1 to S, 1 being the brilliant stone. A few sorts of stones are accessible for nothing out of pocket in different pieces of the city. The city comprises of N intersections numbered from 1 to N and M two-route boulevards between sets of unmistakable intersections. At every intersection, at least zero sorts of stones are accessible in boundless flexibly.

Shockingly, the brilliant stone isn’t accessible anyplace. Fortunately, Leopold is somewhat of a performer and realizes how to consolidate a gathering of stones and transform them into another stone. For instance, one of his plans could deliver a brilliant stone out of one silver stone and two marble stones. He could gather those stones in a portion of the intersections on the off chance that they are accessible, or he could utilize a portion of his numerous different plans to create any of those stones. Officially, Leopold has R plans, where a formula is in the structure (a1, a2, …, ak) – > b for some k ≥ 1. On the off chance that Leopold has accumulated k stones of types a1, a2, …, and ak at a specific intersection, he can decide to apply the formula and transform these stones into one stone of type b.

Leopold likes confuses substantially more than physical action, in this manner, he would not like to haul stones around the city superfluously. Conveying a stone along a road costs him one unit of vitality. Leopold can convey close to each stone in turn, in any case, he can drop off a stone at any intersection and get it later whenever.

What is the base measure of vitality Leopold must spend to deliver one brilliant stone? Leopold is exceptionally terrified of huge numbers. In the event that the appropriate response is more prominent than or equivalent to 1012, print – 1.

Information

The primary line of the info gives the quantity of experiments T. T experiments follow. The primary line of each experiment comprises of four whole numbers N, M, S, and R giving the quantity of intersections, boulevards, stone sorts, and plans, separately. The accompanying M lines depict the guide of the city. The I-th of these lines contains two unmistakable whole numbers Ui and Vi meaning the pair of intersections associated by the I-th road.

The resulting N lines portray the kinds of stones accessible in every intersection. The I-th of these lines begins with the quantity of stone kinds Ci accessible in I-th intersection followed by Ci unmistakable whole numbers in the range [2, S] listing the stone sorts. The brilliant stone is constantly numbered 1 and isn’t accessible.

The last R lines of each experiment depict Leopold’s enchantment plans. The I-th of these lines begins with the quantity of fixing stones Ki required for the I-th formula followed by Ki not really unmistakable whole numbers in the range [2, S] identifying the sorts of essential fixings. The I-th line closes with a whole number in the range [1, S], which is the kind of the subsequent stone in the wake of applying the I-th formula. For instance 3 6 5 6 3 signifies a formula that requires two stones of type 6, one stone of type 5, and produces a stone of type 3.

It is ensured that it is conceivable to create a brilliant stone in every one of the experiments.

Yield

For each experiment, yield one line containing Case #x: y, where x is the experiment number (beginning from 1) and y is the response for the experiment x, specifically, the base measure of vitality Leopold must spend to create one brilliant stone. On the off chance that the appropriate response is more noteworthy than or equivalent to 1012, print – 1.