Under construction

Still being built. Things may change

Soju

Time limit3sMemory limit128 MB

Summary
Given two point sets with every point of the first west of every point of the second, find the smallest Manhattan distance between a point from each set.
Level

Medium7 of 10

Topics
Geometry, Sorting
Solved
No attempts yet

Problem

Two rival companies, IC and PC, share the soju market in Korea. Soju is a well-known Korean distilled liquor that goes well with Korean cuisine. The two companies have an agreement about where their branches may sit: every IC branch must lie to the west of every PC branch. In other words, all IC branches have a smaller xx-coordinate than any PC branch.

At the beginning of each month the companies check whether the agreement still holds. To do so they want the distance of the closest pair of branches, one from each company. The distance between two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is defined as ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

Example layout of IC and PC branches

Figure 1. Blue disks are IC branches and red squares are PC branches. The distance of a closest pair between them is 5.

You are given two sets of points, II and PP, representing the branches of IC and PC respectively. Every point in II has a smaller xx-coordinate than any point in PP. Write a program that computes the minimum distance over all pairs (i,p)(i, p) with i∈Ii \in I and p∈Pp \in P.

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 an integer nn, the number of points in II (1≤n≤1000001 \le n \le 100000). Each of the following nn lines contains the xx-coordinate and yy-coordinate of one point of II. The next line contains an integer mm, the number of points in PP (1≤m≤1000001 \le m \le 100000). Each of the following mm lines contains the xx-coordinate and yy-coordinate of one point of PP. All coordinates are integers between −106-10^6 and 10610^6 inclusive. Note that every point of II has a smaller xx-coordinate than any point of PP.

Output

Your program writes to standard output. Print exactly one line for each test case. Each line contains a single integer, the distance of a closest pair of points, one from II and one from PP.

Examples3

  1. Example 1

    Input
    2
    10
    -16 -4
    -1 -3
    -9 -1
    -4 -10
    -11 -6
    -20 4
    -13 6
    -3 -10
    -19 -1
    -12 -4
    10
    8 2
    10 3
    10 10
    20 -3
    20 3
    16 2
    3 -5
    14 -10
    8 -2
    14 0
    10
    -3 39
    -2 -28
    -1 20
    -3 11
    -3 45
    -2 -44
    -1 -47
    -5 -35
    -5 -19
    -5 -45
    10
    27 5
    28 0
    28 5
    21 5
    2 3
    13 -1
    16 -2
    20 -2
    33 -3
    27 1
    
    Expected output
    6
    13
    
  2. Example 2

    Input
    1
    1
    -5 3
    1
    4 3
    
    Expected output
    9
    
  3. Example 3

    Input
    1
    3
    -1 0
    -3 5
    -8 -6
    3
    2 0
    6 -4
    9 7
    
    Expected output
    3