Hot Dogs in Manhattan

Time limit2sMemory limit128 MB

Summary
Given existing stands on a w by h grid, choose two empty intersections maximizing the smaller of the two minimum distances to all other stands.
Level

Hard8 of 10

Topics
Binary search, Geometry, Brute force, Math
Solved
No attempts yet

Problem

Two friends, Barack and Mitt, each want to open a hot dog stand in Manhattan, and they are looking for the two best locations.

Both want to place their stand at an intersection for maximum exposure. Manhattan already has many existing stands, all located at intersections. A stand placed close to another stand (including the other friend's new stand) attracts fewer customers, so they want their stands to be as far as possible from every other stand.

Model Manhattan as a finite square grid of ww vertical streets and hh horizontal streets. Vertical streets are at x=0,1,…,w−1x = 0, 1, \dots, w-1 and horizontal streets at y=0,1,…,h−1y = 0, 1, \dots, h-1. Consecutive parallel streets are one unit apart, so the distance between intersections (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) is ∣x1−x2∣+∣y1−y2∣|x_1 - x_2| + |y_1 - y_2|.

The privacy of an intersection is the minimum distance from it to every other stand. Once both new stands are placed, each new stand also counts as a stand for the other, so the privacy of Barack's location depends on the distance to Mitt's location and vice versa. Barack and Mitt want to choose two intersections so that the smaller of their two privacies is as large as possible. Output that maximum value.

Input

The first line contains one positive integer: the number of test cases, at most 100100. Each test case is given as follows:

  • One line with three space-separated integers nn, ww, and hh (0≤n≤10000 \le n \le 1000 and 2≤w,h≤10002 \le w, h \le 1000): the number of existing stands and the number of vertical and horizontal streets.
  • nn lines, each with two space-separated integers xix_i and yiy_i (0≤xi<w0 \le x_i < w and 0≤yi<h0 \le y_i < h): the intersection of the ii-th existing stand.

All existing stands are at distinct intersections, and at least two intersections have no stand.

Output

For each test case, output one line with a single integer: the maximum privacy that both Barack and Mitt can simultaneously achieve.

Note

In the first test case there is one existing stand at (0,1)(0, 1) on a 4×44 \times 4 grid. Placing the two new stands at (2,3)(2, 3) and (3,0)(3, 0) gives each a privacy of 44: both are at distance at least 44 from the existing stand, and they are at distance 44 from each other. No placement does better, so the answer is 44.

When there is no existing stand, the two new stands can be put at opposite corners, so the answer is simply (w−1)+(h−1)(w-1) + (h-1).

Examples1

  1. Example 1

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