Color Tunnels

No attempts yetTime limit1sMemory limit128 MB

Problem

A toy factory paints its products by moving them through colored tunnels. To reach a desired final color, a product must be painted by an ordered sequence of colors.

Each tunnel is a straight line segment of a fixed color, placed somewhere in the plane. A single color may be produced by several tunnels, and different tunnels may share the same color. To be painted with a color, the product must travel through a tunnel of that color from one endpoint to the other (a full traversal); the direction of travel does not matter.

Formally, an unpainted product starts at a given source point and must be delivered to a given destination point after being painted, in order, with the colors c1,c2,,cnc_1, c_2, \ldots, c_n. The product must therefore traverse tunnels t1,t2,,tnt_1, t_2, \ldots, t_n (in this order) such that tunnel tit_i has color cic_i. It may also pass through or cross other tunnels along the way; only the chosen subsequence t1,,tnt_1, \ldots, t_n has to match the required colors. Between traversals the product moves along straight line segments through the obstacle-free plane. The path may cross itself or merely intersect a tunnel without traversing it (which does not count as painting), and the same tunnel may be traversed more than once.

Compute the length of the shortest such path from the source to the destination.

Input

The first line contains an integer tt (1t201 \le t \le 20), the number of test cases. Each test case is given as follows.

  • One line with four real numbers xs ys xt ytx_s\ y_s\ x_t\ y_t: the coordinates of the source (xs,ys)(x_s, y_s) and of the destination (xt,yt)(x_t, y_t).
  • One line describing the color sequence: an integer mm (1m301 \le m \le 30) giving its length, followed by mm integers c1,,cmc_1, \ldots, c_m (each in [1,100][1, 100]), the required colors in order.
  • One line with an integer nn (1n601 \le n \le 60), the number of tunnels.
  • nn lines, each with five numbers x1 y1 x2 y2 cx_1\ y_1\ x_2\ y_2\ c: the endpoints (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) (real numbers) and the color cc (an integer in [1,100][1, 100]) of one tunnel.

Every color that appears in a sequence is produced by at least one tunnel, so a valid path always exists.

Output

For each test case, print a single line containing the minimum total travel length from the source to the destination that traverses tunnels matching the required color order, rounded to exactly three digits after the decimal point.