#d2880. 重建
重建
Reconstruction
Problem Description
After the earthquake in region , all villages suffered some damage, but the earthquake had little effect on the roads. However, before a village is rebuilt, none of the roads connected to an unrebuilt village can be used. In other words, only roads connecting two rebuilt villages can be used, and one can only reach rebuilt villages.
You are given the number of villages in region (villages are numbered to ) and the lengths of all bidirectional roads. You are also given the reconstruction completion time of the -th village; you may assume all villages start rebuilding simultaneously and are completed on day , and can be used for traffic on that same day. If is , it means the earthquake caused no damage to this region and it can be used from the very beginning.
Then there are queries . For each query you must answer: on day , what is the length of the shortest path from village to village ? If, going through some already-rebuilt villages, no path from village to village can be found, or if village or village is not yet rebuilt by day , then return .
Input Format
The first line contains two positive integers , the number of villages and the number of roads.
The second line contains non-negative integers , the reconstruction completion times of each village. The data guarantees .
The next lines each contain non-negative integers , where is a positive integer not exceeding , meaning there is a road of length connecting villages and . It is guaranteed that , and that for any pair of villages there is at most one road.
The next line (that is, line ) contains a positive integer , the number of queries.
The next lines each contain non-negative integers , asking for the length of the shortest path from village to village on day . The data guarantees that is non-decreasing.
Output Format
Output lines in total. For each query , output the corresponding answer — the length of the shortest path from village to village on day . If on day no path from village to village can be found, or village or village is not yet rebuilt, output .
Sample Input
4 5
1 2 3 4
0 2 1
2 3 1
3 1 2
2 1 4
0 3 5
4
2 0 2
0 1 2
0 1 3
0 1 4
Sample Output
-1
-1
5
4
Constraints
, , ; all integers involved in the input do not exceed .