## GUPTA MECHANICAL

IN THIS WEBSITE I CAN TELL ALL ABOUT TECH. TIPS AND TRICKS APP REVIEWS AND UNBOXINGS ALSO TECH. NEWS .............

# [Solution] Field Photography Codeforces Solution

F. Field Photography
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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×{10}^{100}$ with $N$ rows and ${10}^{100}$ columns. The rows are numbered from $1$ to $N$ from north to south, the columns are numbered from $1$ to ${10}^{100}$ from west to east. The tile in row $r$ and column $c$ is denoted as $\left(r,c\right)$.

There are $N$ provinces that participate in OSN 2019. Initially, each contestant who represents province $i$ stands in tile $\left(i,p\right)$ for each $p$ satisfying ${L}_{i}\le p\le {R}_{i}$. So, we can see that there are ${R}_{i}-{L}_{i}+1$ contestants who represent province $i$.

Pak Chanek has a variable $Z$ that is initially equal to $0$. In one operation, Pak Chanek can choose a row $i$ and a positive integer $k$. Then, Pak Chanek will do one of the two following possibilities:

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

After each operation, the value of $Z$ will change into , with $\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.

Solution Click Below:-  👉
👇👇👇👇👇

There are $Q$ questions. For the $j$-th question, you are given a positive integer ${W}_{j}$, Pak Chanek must do zero or more operations so that the final value of $Z$ is exactly ${W}_{j}$. Define $M$ as the biggest number such that after all operations, there is at least one column that contains exactly $M$ contestants. For each question, you must find the biggest possible $M$ 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.

Input

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

The $i$-th of the next $N$ lines contains two integers ${L}_{i}$ and ${R}_{i}$ ($1\le {L}_{i}\le {R}_{i}\le {10}^{9}$) describing the positions of the contestants who represent province $i$.

The next line contains a single integer $Q$ ($1\le Q\le {10}^{5}$) — the number of questions.

The $j$-th of the next $Q$ lines contains a single integer ${W}_{j}$ ($1\le {W}_{j}\le {10}^{9}$) — the required final value of $Z$ for the $j$-th question.

Output

Output $Q$ lines, with the $j$-th line containing an integer that is the answer to the $j$-th question.