IP Geolocation Table
You are given a geo-IP table: a list of IPv4 address blocks, each owned by one country, followed by a batch of queries asking which block (if any) owns a given address.
An IPv4 address is written as four decimal octets separated by dots, a.b.c.d, where each of
a, b, c, d is an integer from 0 to 255 with no leading zeros (so 7 is allowed, 07 is
not; 0 is allowed). Internally, convert it to the 32-bit integer a·2^24 + b·2^16 + c·2^8 + d.
Each block is written as exactly one line in one of two forms:
- Range form:
IP1 - IP2 CODE, meaning every address fromIP1toIP2inclusive (as 32-bit integers, soIP1 ≤ IP2). There may be any number of space characters (zero or more, at most 20) on each side of the-, independently — so10.0.0.0-10.0.0.9 US,10.0.0.0 - 10.0.0.9 USand10.0.0.0 -10.0.0.9 USare all valid and equivalent. Exactly one space separatesIP2fromCODE. - CIDR form:
IP/k CODE, meaning the block of 2^(32-k) addresses obtained by maskingIP's 32-bit integer with the topkbits (0 ≤ k ≤ 32, no leading zeros, sok = 0is the entire address space andk = 32is a single address). Exactly one space separatesIPfromCODEand no space surrounds the/.
CODE is exactly two uppercase English letters (A–Z). It is never the string ??, which is
reserved below to mean "no owner".
The n blocks are given in increasing order of their starting address and are pairwise
non-overlapping: if block i covers integers [s_i, e_i], then s_1 ≤ e_1 < s_2 ≤ e_2 < ... < s_n ≤ e_n. There may be gaps between consecutive blocks (addresses owned by nobody).
Input
The first line contains n (0 ≤ n ≤ 10^5), the number of blocks.
Each of the next n lines describes one block, in the range form or the CIDR form above.
The next line contains q (0 ≤ q ≤ 2·10^5), the number of queries.
Each of the next q lines contains one IPv4 address to look up.
Output
For each query, in order, print one line: the CODE of the block that contains it, or ?? if no
block contains it.
Notes
In the first example, 1.0.1.0/24 covers 1.0.1.0 through 1.0.1.255. The address 1.0.2.0
falls in the gap between that block and 2.0.0.0 - 2.0.0.9, so it prints ??. The address
2.0.0.9 is the last address of the third block, so it still counts as owned.
In the second example, 10.0.0.6 -10.0.0.10 DE has a space before the dash but none after it —
both are allowed. 10.0.0.5/32 owns only the single address 10.0.0.5.