Traffic Lights

Find the fastest drive from intersection S to D when each intersection only lets cars arriving from one road pass during each P-second window.

Medium7Shortest pathGraphNo attempts yetTime limit1sMemory limit256 MB

Problem

Dijkstra's algorithm finds a shortest path between a source and a target in a graph whose edges all have non-negative weights. If the vertices are intersections and each edge is the length of a road joining two intersections, the algorithm gives a shortest route between two intersections.

Real roads have a traffic light at every intersection, so a car cannot pass through an intersection whenever it likes. Taking a detour off the shortest route sometimes reaches the destination sooner.

A traffic light changes its signal every PP seconds. Let the intersections joined to intersection ii be numbered x1<x2<<xnx_1 < x_2 < \cdots < x_n. During the first PP seconds only cars that came from x1x_1 may pass through ii toward x2,,xnx_2, \ldots, x_n, and during the next PP seconds only cars that came from x2x_2 may pass through ii toward x1,x3,,xnx_1, x_3, \ldots, x_n. The right to pass through ii moves over the neighbours in increasing order of number, PP seconds each, and after n×Pn \times P seconds it returns to x1x_1. A car that came from xkx_k may leave toward any neighbour except xkx_k.

(a)(b)(c)

For example, suppose intersection 3 is joined to intersections 1, 4 and 5. From 0 seconds up to PP seconds, picture (a) applies and cars that reached intersection 3 over the road from intersection 1 may take a road to another intersection. From PP seconds up to 2P2P seconds, picture (b) applies and cars that came from intersection 4 may move on. From 2P2P seconds up to 3P3P seconds, picture (c) applies and cars that came from intersection 5 may go to intersection 1 or intersection 4. From 3P3P seconds up to 4P4P seconds the state of (a) returns.

Find the minimum time for a car to travel from its origin to its destination under these conditions.

  • A car travels 1 unit of length per second.
  • The origin and the destination are both intersections.
  • Every traffic light starts working at the moment the car departs.
  • The car waits for no signal at the origin intersection and at the destination intersection.
  • No road leads from an intersection back to the same intersection.

For example, take the road network above with intersection 1 as the origin and intersection 4 as the destination. A car leaving intersection 1 reaches intersection 3 at second 10. If the period of intersection 3 is 2 seconds, then from second 10 up to second 12 only cars that came from intersection 5 may use the roads, so a car that came from intersection 1 waits until second 12. Driving from intersection 1 to intersection 4 therefore takes 14 seconds.

Given every road length and every signal period, find the minimum time to drive from the origin intersection to the destination intersection.

Input

The first line has the number of test cases TT (1T101 \le T \le 10).

The first line of each test case has the number of intersections NN (1N1051 \le N \le 10^5), the number of roads MM (0M1050 \le M \le 10^5), the number of the origin intersection SS (1SN1 \le S \le N) and the number of the destination intersection DD (1DN1 \le D \le N).

Each of the next MM lines has aa, bb, cc (1a,bN1 \le a, b \le N, aba \ne b, 1c1051 \le c \le 10^5). It means that a two-way road of length cc joins intersection aa and intersection bb.

The next line has the signal periods P1,P2,,PNP_1, P_2, \ldots, P_N (1Pi1001 \le P_i \le 100) of the intersections, separated by single spaces.

At most one road joins any two intersections.

Output

For each test case, print on its own line the minimum time to drive from the origin intersection to the destination intersection. The result can exceed the range of a 32-bit integer, so a 64-bit integer type is recommended. If no route reaches the destination intersection, print -1.