Sequence and Queries 7

Time limit4sMemory limit512 MB

Summary
For each query range, find the longest subarray whose sum is divisible by K.
Level

Hard8 of 10

Topics
Prefix sum, Divide and conquer, Hash map
Solved
No attempts yet

Problem

You are given a sequence A1,A2,…,ANA_1, A_2, \dots, A_N of length NN and an integer KK. Write a program that answers the following query.

  • l r: print the length of the longest contiguous subsequence with l≤i≤j≤rl \le i \le j \le r and (Ai+Ai+1+⋯+Aj) mod K=0(A_i + A_{i+1} + \cdots + A_j) \bmod K = 0. If no subsequence satisfies the condition, the length is 0.

Input

The first line contains the size of the sequence NN (1≤N≤100 0001 \le N \le 100\,000) and KK (2≤K≤1 000 0002 \le K \le 1\,000\,000).

The second line contains A1,A2,…,ANA_1, A_2, \dots, A_N. (1≤Ai≤1 000 000 0001 \le A_i \le 1\,000\,000\,000)

The third line contains the number of queries MM (1≤M≤100 0001 \le M \le 100\,000).

Each of the next MM lines contains one query ll, rr. (1≤l≤r≤N1 \le l \le r \le N)

Output

Print the answer to each query on its own line.

Examples1

  1. Example 1

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