Shortest Bridge

Given two polygonal riverbanks and points s and t on opposite sides, minimize the bridge length between the banks, then the road lengths from s and t to its endpoints.

Hard8GeometryBrute forceImplementationMathNo attempts yetTime limit5sMemory limit512 MB

Problem

The city is a square 1,000 units on a side. A large river runs through it from north to south and splits the city into exactly two parts, the west part and the east part.

The mayor has decided to build a highway from a point ss in the west part to a point tt in the east part. A highway is one bridge over the river plus two roads. One road joins ss to the west end of the bridge, and the other joins tt to the east end. The bridge is a straight segment between a point on the west riverside and a point on the east riverside. A road does not have to be a straight line, but the length of its intersection with the river must be zero.

To keep the cost down, the mayor builds a highway that meets both of these conditions.

  • A bridge costs more than a road, so the length of the bridge joining the west part and the east part must be as short as possible.
  • Under that condition, the sum of the lengths of the two roads must be as small as possible.

Write a program that computes the total length of a highway meeting both conditions.

Input

The input is a single test case in the following format.

sx sy tx ty
N
wx1 wy1
:
:
wxN wyN
M
ex1 ey1
:
:
exM eyM

A point in the city is written as a coordinate (x,y)(x, y), where xx is the distance from the west side and yy is the distance from the north side.

The first line contains four integers sxs_x, sys_y, txt_x, tyt_y (0sx,sy,tx,ty10000 \le s_x, s_y, t_x, t_y \le 1000). Point ss is at (sx,sy)(s_x, s_y) and point tt is at (tx,ty)(t_x, t_y). The next line contains an integer NN (2N202 \le N \le 20), the number of points that make up the west riverside. Each of the next NN lines contains two integers wxiwx_i and wyiwy_i (0wxi,wyi10000 \le wx_i, wy_i \le 1000), and the ii-th point of the west riverside is (wxi,wyi)(wx_i, wy_i). The west riverside is the polygonal line built from the segments between (wxi,wyi)(wx_i, wy_i) and (wxi+1,wyi+1)(wx_{i+1}, wy_{i+1}) for all 1iN11 \le i \le N-1. The next line contains an integer MM (2M202 \le M \le 20), the number of points that make up the east riverside. Each of the next MM lines contains two integers exiex_i and eyiey_i (0exi,eyi10000 \le ex_i, ey_i \le 1000), and the ii-th point of the east riverside is (exi,eyi)(ex_i, ey_i). The east riverside is built the same way.

The input satisfies the following conditions.

  • wy1wy_1 and ey1ey_1 are 0, and wyNwy_N and eyMey_M are 1,000.
  • Neither polygonal line intersects itself.
  • The west riverside and the east riverside do not meet.
  • Point ss lies in the west part of the city. That is, ss lies in the region bounded by the sides of the square and the west polygonal line that does not contain the points of the east riverside.
  • Point tt lies in the east part of the city. That is, tt lies in the region bounded by the sides of the square and the east polygonal line that does not contain the points of the west riverside.
  • Each polygonal line meets the square only at its two end points. In other words, 0<wxi,wyi<10000 < wx_i, wy_i < 1000 holds for 2iN12 \le i \le N-1, and 0<exi,eyi<10000 < ex_i, ey_i < 1000 holds for 2iM12 \le i \le M-1.

Output

Print the length of the bridge and the total length of the highway on one line, separated by a single space. The total length of the highway is the bridge plus the two roads. Print both values with exactly four digits after the decimal point.