Under construction

Still being built. Things may change

Rollback

Time limit2sMemory limit512 MB

Summary
For each online query (l, k), find the smallest r such that the subarray a[l..r] contains at least k distinct server numbers, or report 0 if impossible.
Level

Hard8 of 10

Topics
Binary search, Prefix sum, Array, Sorting
Solved
No attempts yet

Problem

Sergey works as a system administrator at a very large company. Naturally, his duties include backing up the information stored on various servers and <> to a previous version when problems arise.

Right now Sergey is fighting a shortage of space for storing recovery information. He decided to move part of the information to new servers. Unfortunately, if something goes wrong during the move, he will not be able to roll back, so the move has to be planned carefully.

At the moment Sergey has nn recovery points of various servers, numbered from 1 to nn. The recovery point with number ii can roll back server a_ia\_i. Sergey decided to split the move into stages, and at each stage, if problems arise, the recovery points with numbers l,l+1,…,rl, l + 1, \ldots, r will be available.

To plan the data move optimally, Sergey needs to answer queries: for a given ll, what is the minimum rr such that at least kk distinct servers are available during the move?

Help Sergey.

Input

The first line of the input file contains two integers nn and mm separated by a space: the number of recovery points and the number of servers (1≤n,m≤100 0001 \le n, m \le 100\,000). The second line contains nn integers a_1,a_2,…,a_na\_1, a\_2, \ldots, a\_n: the server numbers that correspond to the recovery points (1≤a_i≤m1 \le a\_i \le m).

The third line of the input file contains qq: the number of queries to process (1≤q≤100 0001 \le q \le 100\,000). While processing the queries, you must maintain a number pp, initially equal to 0. Each query is given by a pair of numbers x_ix\_i and y_iy\_i; use them to obtain the query data as follows: l_i=((x_i+p) mod n)+1l\_i = \left((x\_i + p) \bmod n\right) + 1, k_i=((y_i+p) mod m)+1k\_i = \left((y\_i + p) \bmod m\right) + 1 (1≤l_i,x_i≤n1 \le l\_i, x\_i \le n, 1≤k_i,y_i≤m1 \le k\_i, y\_i \le m). Let the answer to the ii-th query be rr. After completing this query, assign pp the value rr.

Output

For each query, output one number: the minimum rr sought, or 0 if no such rr exists.

Examples1

  1. Example 1

    Input
    7 3
    1 2 1 3 1 2 1
    4
    7 3
    7 1
    7 1
    2 2
    
    Expected output
    1
    4
    0
    6