Under construction

Still being built. Things may change

Sequence and Queries 40

Time limit2sMemory limit512 MB

Summary
Given a sequence A and modulus M, each query shifts every value by d modulo M and asks for the start index of the k-th smallest suffix in lexicographic order.
Level

Hard8 of 10

Topics
String, Sorting, Binary search
Solved
No attempts yet

Problem

You are given a sequence A1,A2,…,ANA_1, A_2, \ldots, A_N of length NN and an integer MM. Each query consists of two integers dd and kk. For each query, perform the following steps in order.

  • Build a new sequence Bi=(Ai+d) mod MB_i = (A_i + d) \bmod M for 1≤i≤N1 \le i \le N.
  • For every ii (1≤i≤N1 \le i \le N), define the ii-th suffix as Bi,Bi+1,…,BNB_i, B_{i+1}, \ldots, B_N.
  • Output the suffix number of the sequence BB that is kk-th smallest in lexicographic order.

Input

The first line contains NN and MM.

The second line contains A1,A2,…,ANA_1, A_2, \ldots, A_N.

The third line contains the number of queries QQ.

Each of the next QQ lines contains one query, the integers dd and kk.

Output

For each query, print on its own line the suffix number of the kk-th smallest sequence BB in lexicographic order.

Constraints

  • 1≤N≤1051 \le N \le 10^5
  • 1≤M≤1091 \le M \le 10^9
  • 0≤Ai<M0 \le A_i < M
  • 1≤Q≤5⋅1051 \le Q \le 5 \cdot 10^5
  • 0≤d≤1090 \le d \le 10^9
  • 1≤k≤N1 \le k \le N

Hint

In the first query, the sequence is B=[5,2,0,5,5]B = [5, 2, 0, 5, 5]. Sorting all suffixes in lexicographic order gives [0,5,5][0, 5, 5], [2,0,5,5][2, 0, 5, 5], [5][5], [5,2,0,5,5][5, 2, 0, 5, 5], [5,5][5, 5]. The suffix in 4th place has number 1.

Examples1

  1. Example 1

    Input
    5 6
    1 4 2 1 1
    3
    4 4
    2 3
    1 1
    
    Expected output
    1
    1
    5