#P1725F. Field Photography

    ID: 7903 Type: RemoteJudge 1000ms 256MiB Tried: 0 Accepted: 0 Difficulty: (None) Uploaded By: Tags>bitmasksdata structuressortings

Field Photography

No submission language available for this problem.

Description

Pak Chanek is traveling to Manado. It turns out that OSN (Indonesian National Scientific Olympiad) 2019 is being held. The contestants of OSN 2019 are currently lining up in a field to be photographed. The field is shaped like a grid of size N×10100N \times 10^{100} with NN rows and 1010010^{100} columns. The rows are numbered from 11 to NN from north to south, the columns are numbered from 11 to 1010010^{100} from west to east. The tile in row rr and column cc is denoted as (r,c)(r,c).

There are NN provinces that participate in OSN 2019. Initially, each contestant who represents province ii stands in tile (i,p)(i, p) for each pp satisfying LipRiL_i \leq p \leq R_i. So, we can see that there are RiLi+1R_i-L_i+1 contestants who represent province ii.

Pak Chanek has a variable ZZ that is initially equal to 00. In one operation, Pak Chanek can choose a row ii and a positive integer kk. Then, Pak Chanek will do one of the two following possibilities:

  • Move all contestants in row ii exactly kk tiles to the west. In other words, a contestant who is in (i,p)(i, p) is moved to (i,pk)(i, p-k).
  • Move all contestants in row ii exactly kk tiles to the east. In other words, a contestant who is in (i,p)(i, p) is moved to (i,p+k)(i, p+k).

After each operation, the value of ZZ will change into Z OR kZ \text{ OR } k, with OR\text{OR} being the bitwise OR operation. Note that Pak Chanek can do operations to the same row more than once. Also note that Pak Chanek is not allowed to move contestants out of the grid.

There are QQ questions. For the jj-th question, you are given a positive integer WjW_j, Pak Chanek must do zero or more operations so that the final value of ZZ is exactly WjW_j. Define MM as the biggest number such that after all operations, there is at least one column that contains exactly MM contestants. For each question, you must find the biggest possible MM for all sequences of operations that can be done by Pak Chanek. Note that the operations done by Pak Chanek for one question do not carry over to other questions.

The first line contains a single integer NN (1N1051 \leq N \leq 10^5) — the number of rows in the grid and also the number of provinces that participate in OSN 2019.

The ii-th of the next NN lines contains two integers LiL_i and RiR_i (1LiRi1091 \leq L_i \leq R_i \leq 10^9) describing the positions of the contestants who represent province ii.

The next line contains a single integer QQ (1Q1051 \leq Q \leq 10^5) — the number of questions.

The jj-th of the next QQ lines contains a single integer WjW_j (1Wj1091 \leq W_j \leq 10^9) — the required final value of ZZ for the jj-th question.

Output QQ lines, with the jj-th line containing an integer that is the answer to the jj-th question.

Input

The first line contains a single integer NN (1N1051 \leq N \leq 10^5) — the number of rows in the grid and also the number of provinces that participate in OSN 2019.

The ii-th of the next NN lines contains two integers LiL_i and RiR_i (1LiRi1091 \leq L_i \leq R_i \leq 10^9) describing the positions of the contestants who represent province ii.

The next line contains a single integer QQ (1Q1051 \leq Q \leq 10^5) — the number of questions.

The jj-th of the next QQ lines contains a single integer WjW_j (1Wj1091 \leq W_j \leq 10^9) — the required final value of ZZ for the jj-th question.

Output

Output QQ lines, with the jj-th line containing an integer that is the answer to the jj-th question.

Sample Input 1

3
1 5
10 11
8 8
2
12
5

Sample Output 1

2
3

Note

For the 11-st question, Pak Chanek can do the following operations to get M=2M=2:

  • Move all contestants in row 22 to the east by 44 tiles. ZZ changes into 0 OR 4=40 \text{ OR } 4 = 4.
  • Move all contestants in row 11 to the east by 1212 tiles. ZZ changes into 4 OR 12=124 \text{ OR } 12 = 12.

Now, columns 1414 and 1515 each contains exactly 22 contestants.

For the 22-nd question, Pak Chanek can do the following operations to get M=3M=3:

  • Move all contestants in row 33 to the east by 44 tiles. ZZ changes into 0 OR 4=40 \text{ OR } 4 = 4.
  • Move all contestants in row 33 to the west by 11 tiles. ZZ changes into 4 OR 1=54 \text{ OR } 1 = 5.
  • Move all contestants in row 11 to the east by 55 tiles. ZZ changes into 5 OR 5=55 \text{ OR } 5 = 5.
  • Move all contestants in row 11 to the east by 55 tiles. ZZ changes into 5 OR 5=55 \text{ OR } 5 = 5.

Now, column 1111 contains exactly 33 contestants.

The following is an illustration of the example operations for the 22-nd question.