#SGM0000073. Xây dựng quân đội (Army Creation)

Xây dựng quân đội (Army Creation)

Army Creation

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem

Warrior ii has type aia_i. From a decoded interval [l,r][l,r], choose as many warriors as possible while taking at most kk of each type. Decode l=((x+last)mod n)+1 and r=((y+last)mod n)+1, then swap if needed.

Input

The first line contains n,kn,k, the second line the types, the third line qq, followed by qq encoded pairs x,yx,y.

Output

Print each maximum army size and use it as last for the next query.

Subtasks

Subtask 1 (20%)

  • n≤30n\le 30, number of queries ≤30\le 30.
  • All other conditions are the same as Subtask 3.

Subtask 2 (30%)

  • n≤3000n\le 3000, number of queries ≤3000\le 3000.
  • All other conditions are the same as Subtask 3.

Subtask 3 (50%)

  • 1≤n,k≤1051\le n,k\le10^5
  • 1≤ai≤1051\le a_i\le10^5
  • 1≤q≤1051\le q\le10^5
  • 1≤x,y≤n1\le x,y\le n

Example

Input

6 2
1 1 1 2 2 2
5
1 6
4 3
1 1
2 6
2 6

Output

2
4
1
3
2

Explanation

With k=2k=2, at most two warriors of every type can be chosen. Intervals are decoded sequentially using the previous answer.