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,…,cn. The product must therefore traverse tunnels t1,t2,…,tn (in this order) such that tunnel ti has color ci. It may also pass through or cross other tunnels along the way; only the chosen subsequence t1,…,tn 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.
The first line contains an integer t (1≤t≤20), the number of test cases. Each test case is given as follows.
Every color that appears in a sequence is produced by at least one tunnel, so a valid path always exists.
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.