Shortest Path Query

아직 제출이 없습니다시간 제한2초메모리 제한1024 MB

문제

You will be given a directed acyclic graph with nn vertices, labeled by 1,2,,n1,2,\ldots,n. There are mm edges in the graph, each edge is either black or white. It is guaranteed that you can reach every vertex from the 11-st vertex.

You will be given qq queries. In the ii-th query, you will be given three integers a_ia\_i, b_ib\_i and x_ix\_i. You need to report the length of the shortest path from the 11-st vertex to the x_ix\_i-th vertex if we regard the length of each black edge as a_ia\_i and regard the length of each white edge as b_ib\_i.

입력

The first line of the input contains two integers nn and mm (1n50,0001 \leq n\leq 50\\,000, 1m100,0001\leq m\leq 100\\,000), denoting the number of vertices and the number of directed edges.

In the next mm lines, the ii-th line contains three integers u_iu\_i, v_iv\_i and c_ic\_i (1u_i\<v_in1\leq u\_i\<v\_i\leq n, v_iu_i1000v\_i-u\_i\leq 1000, 0c_i10\leq c\_i\leq 1), describing a directed edge from the u_iu\_i-th vertex to the v_iv\_i-th vertex. When c_i=0c\_i=0, its color is black, and when c_i=1c\_i=1, its color is white.

The next line contains a single integer qq (1q50,0001 \leq q \leq 50\\,000), denoting the number of queries.

Each of the next qq lines contains three integers a_ia\_i, b_ib\_i and x_ix\_i (1a_i,b_i10,0001\leq a\_i,b\_i\leq 10\\,000, 1x_in1\leq x\_i\leq n), denoting a query.

It is guaranteed that you can reach every vertex from the 11-st vertex.

출력

For each query, print a single line containing an integer, denoting the length of the shortest path.