Dhaka city is getting crowded and noisy everyday. Certain roads always remain blocked for congestion. In order to convince people avoid shortest routes as it's the number one reason for roads crowded; the city authority has come up with a new plan.
Each junction of the city is marked with a positive integer (≤ 20) denoting the busy-ness of the junction. Whenever someone goes from one junction (the source junction) to another (the destination junction), the city authority gets an amount of money (busy-ness of destination - busy-ness of source)3 (that means the cube of the difference) from the traveler.
Now, the authority has appointed you to find the minimum total amount that can be earned when someone goes from a certain junction (the zero point) to several others.
Input
Input starts with an integer T (≤ 50), denoting the number of test cases.
Each case contains a blank line and an integer n (1 < n ≤ 200) denoting the number of junctions. The next line contains n integers denoting the busyness of the junctions from 1 to n respectively.
The next line contains an integer m, the number of roads in the city. Each of the next m lines (one for each road) contains two junction-numbers (source, destination) that the corresponding road connects (all roads are unidirectional). The next line contains the integer q, the number of queries. The next q lines each contain a destination junction-number. There can be at most one direct road from a junction to another junction.
Output
For each case, print the case number in a single line. Then print q lines, one for each query, each containing the minimum total earning when one travels from junction 1 (the zero point) to the given junction. However, for the queries that gives total earning less than 3, or if the destination is not reachable from the zero point, then print a ?
.
Sample
Sample Input | Sample Output |
---|---|
2 5 6 7 8 9 10 6 1 2 2 3 3 4 1 5 5 4 4 5 2 4 5 2 10 10 1 1 2 1 2 | Case 1: 3 4 Case 2: ? |