Limits
2s, 256 MB

Bob is the owner of a shop named "Color Maker". He has 256 types of watercolor in his shop. The types are numbered from 0 to 255. Bob stores the watercolors in small boxes. One box contains only one type of watercolor and there can be more than one box containing the same type of watercolor. Bob keeps the boxes on a shelf side by side. As he has a limited supply of watercolors, he sells the boxes of watercolors with a condition applied.

Bob arranges another set of sample boxes containing watercolors and keeps them on another shelf side by side. He does not sell these sample boxes.

After that, Bob numbers the boxes. Let the number of boxes on the first shelf is $N$ and the number of the sample boxes is $M$. Then Bob numbers the boxes on the first shelf from 1 to $N$ and the sample boxes from 1 to $M$.

When Alice wants to buy watercolors in this shop, Bob asks her to choose a box. If she chooses the $k$-th box, Bob finds out a position $j$ in the sample boxes and the number of boxes $p$ such that:

$B_k = S_j$, $B_{k+1} = S_{j+1}$, $B_{k+2} = S_{j+2}$, ..., $B_{k+p-1} = S_{j+p-1}$, where $B_i$ represents the $i$-th box on the first shelf, $S_i$ represents the $i$-th box of the sample boxes and $k ≥ 1$, $k+p-1 ≤ N$, $j ≥ 1$ and $j+p-1 ≤ M$. Bob then sells $p$ boxes to Alice.

Surprisingly, Bob notices that there can be multiple ways and multiple values of $p$ possible. Alice demands that among the ways, he has to choose the way in which the value of $p$ is maximized and has to sell $p$ boxes. Since calculating the maximum possible value of p is tough for Alice, she asks you to calculate this on behalf of her.

The first line of the input contains two integers $N$ ($1 ≤ N ≤ 10^6$), $M$ ($1 ≤ M ≤ 10^3$) - the number of boxes on the first shelf and the number of the sample boxes.

The second line contains $N$ space-separated integers. the $i$-th integer represents the type of the watercolor $i$-th box contains.

The third line contains $M$ space-separated integers. the $i$-th integer represents the type of the watercolor $i$-th sample box contains.

The next line contains an integer $Q$ ($1 ≤ Q ≤ 10^6$), the number of the queries. In each of the next $Q$ lines, there will be an integer $k$ ($1 ≤ k ≤ N$), the position of the box Alice chooses.

Other Constraints:

- $0 ≤ \text{Types of Watercolor} ≤ 255$

For each query, print in this format in a single line (without quotes): "Query x: y", where $x$ is the number of the query and $y$ is the maximum possible value of $p$ described in the statement.

Input | Output |
---|---|

6 5 14 25 90 14 81 12 14 14 81 12 0 3 5 2 4 | Query 1: 2 Query 2: 0 Query 3: 3 |

In the third Query, Alice chooses the $4^{th}$ box. So, $k = 4$. To maximize the value of $p$, the best way is to select the position 2 in the sample boxes. So, $j = 2$. Thus, $B_4 = S_2$, $B_5 = S_3$, $B_6 = S_4$. Here, $B_4 = 14$, $B_5 = 81$, $B_6 = 12$ and $S_2 = 14$, $S_3 = 81$, $S_4 = 12$. As 3 boxes are matched in this way, the value of $p$ is 3 here and its the maximum among other ways. $B$ represents the boxes on the first shelf, $S$ represents the sample boxes. |

Login to submit.

79%
Solution Ratio

tmwilliamlin168Earliest,

TurinhstuFastest, 0.2s

mahbubcsejuLightest, 27 MB

borisalShortest, 1501B

Toph uses cookies. By continuing you agree to our Cookie Policy.