#d2880. 重建

重建

Reconstruction

Problem Description

After the earthquake in region BB, 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 NN in region BB (villages are numbered 00 to N−1N - 1) and the lengths of all MM bidirectional roads. You are also given the reconstruction completion time tit_i of the ii-th village; you may assume all villages start rebuilding simultaneously and are completed on day tit_i, and can be used for traffic on that same day. If tit_i is 00, it means the earthquake caused no damage to this region and it can be used from the very beginning.

Then there are QQ queries (x,y,t)(x, y, t). For each query you must answer: on day tt, what is the length of the shortest path from village xx to village yy? If, going through some already-rebuilt villages, no path from village xx to village yy can be found, or if village xx or village yy is not yet rebuilt by day tt, then return −1-1.

Input Format

The first line contains two positive integers N,MN, M, the number of villages and the number of roads.

The second line contains NN non-negative integers t0,t1,…,tN−1t_0, t_1, \dots, t_{N-1}, the reconstruction completion times of each village. The data guarantees t0≤t1≤⋯≤tN−1t_0 \leq t_1 \leq \dots \leq t_{N-1}.

The next MM lines each contain 33 non-negative integers i,j,wi, j, w, where ww is a positive integer not exceeding 1000010000, meaning there is a road of length ww connecting villages ii and jj. It is guaranteed that i≠ji \neq j, and that for any pair of villages there is at most one road.

The next line (that is, line M+3M + 3) contains a positive integer QQ, the number of queries.

The next QQ lines each contain 33 non-negative integers x,y,tx, y, t, asking for the length of the shortest path from village xx to village yy on day tt. The data guarantees that tt is non-decreasing.

Output Format

Output QQ lines in total. For each query (x,y,t)(x, y, t), output the corresponding answer — the length of the shortest path from village xx to village yy on day tt. If on day tt no path from village xx to village yy can be found, or village xx or village yy is not yet rebuilt, output −1-1.

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

N≤200N \leq 200, M≤N×(N−1)/2M \leq N \times (N - 1) / 2, Q≤50000Q \leq 50000; all integers involved in the input do not exceed 100000100000.