Critical 3-Path

No attempts yetTime limit3sMemory limit128 MB

Problem

A PERT (Program Evaluation and Review Technique) chart is a graphical tool used in project management to model the tasks needed to complete a project. It is a directed acyclic graph: each edge represents a task, and the weight of an edge is the time required to perform that task. If an edge (u,v)(u, v) enters vertex vv and an edge (v,w)(v, w) leaves vv, then task (u,v)(u, v) must be finished before task (v,w)(v, w) can start, so a path through the chart is a sequence of tasks that must be carried out in a fixed order. The chart contains no cycle.

A critical path is a longest path in the chart. Its weight is a lower bound on the total time needed to finish every task.

Given six distinct vertices s1,s2,s3,t1,t2,t3s_1, s_2, s_3, t_1, t_2, t_3, a 3-path is defined as follows:

  1. It consists of three paths PiP_i, where each PiP_i goes from sis_i to tit_i for i=1,2,3i = 1, 2, 3.
  2. The paths P1,P2,P3P_1, P_2, P_3 are vertex-disjoint: no vertex belongs to more than one of them.

The length of a 3-path is the sum of the lengths (total edge weight) of P1P_1, P2P_2, and P3P_3. A critical 3-path is a 3-path of maximum length over all 3-paths.

For example, in the graph of the first sample below with s=(3,4,5)s = (3, 4, 5) and t=(15,16,17)t = (15, 16, 17), one critical 3-path is:

  • P1P_1: 3611153 \to 6 \to 11 \to 15
  • P2P_2: 47912164 \to 7 \to 9 \to 12 \to 16
  • P3P_3: 5813175 \to 8 \to 13 \to 17

and its length is 128128.

Given a PERT chart and the six vertices, write a program that outputs the length of the critical 3-path.

Input

The first line contains the number of test cases TT. Each test case begins with a line containing two integers nn and mm (6n1006 \le n \le 100, n1mn(n1)/2n - 1 \le m \le n(n-1)/2), where nn is the number of vertices and mm is the number of edges. Vertices are numbered from 11 to nn. The next line contains six distinct integers s1,s2,s3,t1,t2,t3s_1, s_2, s_3, t_1, t_2, t_3. Each of the following mm lines contains three integers uu, vv, and WW (1W100,0001 \le W \le 100{,}000), describing a directed edge from uu to vv with weight WW. You may assume that u<vu < v for every edge.

Output

For each test case, print a single line containing the length of the critical 3-path formed by P1P_1 (from s1s_1 to t1t_1), P2P_2 (from s2s_2 to t2t_2), and P3P_3 (from s3s_3 to t3t_3). If no such 3-path exists, print 00.