Manhattan distance (L1 distance)

Imagine walking in Manhattan. Streets run north-south and east-west, so you move in axis-aligned blocks. The distance between two intersections is the number of blocks you must walk horizontally plus the number of blocks you must walk vertically. You cannot cut diagonally through buildings.

manhattan.jpg

This “city-block” idea defines the Manhattan distance (also called L1 distance or taxicab distance).

For 2D points and , the Manhattan distance between them would be calculated as:

Taxi Blocks

In a grid city, a taxi needs to choose the closest taxi stand using block distance. You know where the stands are and their names. For each pickup location, send the taxi to the nearest stand by Manhattan distance.

The first line of the input contains a single integer n representing the number of taxi stands.

The next n lines each contain two floating-point numbers x y followed by a stand name (one word without spaces), describing one stand’s position and name.

The next line contains a single integer q representing the number of pickup locations.

The last q lines each contain two floating-point numbers x y for a pickup location.

For each pickup location, print the name of the single nearest stand using Manhattan (L1) distance: |x - xi| + |y - yi|. If several stands are exactly tied for the shortest distance, print the one that appears earliest in the input.

Input

Output

4
0 0 central
2 0 east
0 3 north
-1 -1 south
3
0.5 0.5
2.5 0.2
1 2

central
east
north

3
-2 0 west
2 0 east
0 2 uptown
2
0 0
0 1.1

west
uptown

2
0 0 alpha
0 2 beta
3
0 1
0 1.001
10 -10

alpha
beta
alpha

Constraints

Time limit: 2 seconds

Memory limit: 512 MB

Output limit: 1 MB

To check your solution you need to sign in
Sign in to continue