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 MBA 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)
Let P be a histogram given by the list (v0,v1,…,vn−1) of its n vertices in counterclockwise order along the boundary, where the base segment is (v0,v1). The edge ei is the segment joining the vertices vi and vi+1, for i=0,1,…,n−1 with vn=v0.
A path inside P is a simple path that does not intersect the exterior of P. The length of a path is the sum of the Euclidean lengths of its segments. The distance between two points p and q of P is the length of the shortest path inside P between them. A point on the boundary written p(k,d) is the point on the edge ek whose distance from vk is d.
In the histogram of Figure 1, the shortest path between v0 and q1=p(10,2) is the polygonal chain through v0, v14, v12 and q1 in that order, and its length is 8.595242. The shortest path between v0 and q2=p(1,1) is the segment joining the two points directly, and its length is 15.033296.
You are given a histogram P with n vertices and a set S of m points on the boundary of P. Write a program that finds the distances between v0 and all points of S.
Your program reads from standard input. The first line contains the number of test cases T.
The first line of each test case contains the number of vertices n of the histogram P=(v0,v1,…,vn−1). (4≤n≤100000)
Each of the next n lines contains one vertex, from v0 to vn−1, given as two integers, the x-coordinate and the y-coordinate of that vertex. Both coordinates are between 0 and 1000000, inclusive. The segment (v0,v1) is the base segment.
The next line contains the size m of the set S. (1≤m≤100000)
Each of the next m lines contains one point p(k,d) of S as two integers k and d. (0≤k≤n−1, 0≤d< the length of the edge ek) All points of S are distinct.
Your program writes to standard output. For each test case, print on one line the sum of the distances between v0 and all points of S. 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) and q=(x2,y2) is (x2−x1)2+(y2−y1)2.