Path Inside a Histogram

Compute the sum of shortest inside-polygon distances from the base vertex to many boundary points in a rectilinear histogram.

Hard8GeometryShortest pathNo attempts yetTime limit2sMemory limit256 MB

Problem

A histogram is a simple rectilinear polygon whose boundary consists of two chains. The upper chain is monotone with respect to the horizontal axis, and the lower chain is a single horizontal segment called the base segment.

Figure 1. A histogram and its base segment (v0,v1)(v_0, v_1)

Let PP be a histogram given by the list (v0,v1,,vn1)(v_0, v_1, \dots, v_{n-1}) of its nn vertices in counterclockwise order along the boundary, where the base segment is (v0,v1)(v_0, v_1). The edge eie_i is the segment joining the vertices viv_i and vi+1v_{i+1}, for i=0,1,,n1i = 0, 1, \dots, n-1 with vn=v0v_n = v_0.

A path inside PP is a simple path that does not intersect the exterior of PP. The length of a path is the sum of the Euclidean lengths of its segments. The distance between two points pp and qq of PP is the length of the shortest path inside PP between them. A point on the boundary written p(k,d)p(k, d) is the point on the edge eke_k whose distance from vkv_k is dd.

In the histogram of Figure 1, the shortest path between v0v_0 and q1=p(10,2)q_1 = p(10, 2) is the polygonal chain through v0v_0, v14v_{14}, v12v_{12} and q1q_1 in that order, and its length is 8.5952428.595242. The shortest path between v0v_0 and q2=p(1,1)q_2 = p(1, 1) is the segment joining the two points directly, and its length is 15.03329615.033296.

You are given a histogram PP with nn vertices and a set SS of mm points on the boundary of PP. Write a program that finds the distances between v0v_0 and all points of SS.

Input

Your program reads from standard input. The first line contains the number of test cases TT.

The first line of each test case contains the number of vertices nn of the histogram P=(v0,v1,,vn1)P = (v_0, v_1, \dots, v_{n-1}). (4n1000004 \le n \le 100\,000)

Each of the next nn lines contains one vertex, from v0v_0 to vn1v_{n-1}, given as two integers, the x-coordinate and the y-coordinate of that vertex. Both coordinates are between 00 and 10000001\,000\,000, inclusive. The segment (v0,v1)(v_0, v_1) is the base segment.

The next line contains the size mm of the set SS. (1m1000001 \le m \le 100\,000)

Each of the next mm lines contains one point p(k,d)p(k, d) of SS as two integers kk and dd. (0kn10 \le k \le n-1, 0d<0 \le d < the length of the edge eke_k) All points of SS are distinct.

Output

Your program writes to standard output. For each test case, print on one line the sum of the distances between v0v_0 and all points of SS. Round the sum at the second digit after the decimal point and print exactly one digit after the decimal point.

The Euclidean distance between two points p=(x1,y1)p = (x_1, y_1) and q=(x2,y2)q = (x_2, y_2) is (x2x1)2+(y2y1)2\sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2}.