PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000025. Bổ sung giá trị xuất hiện ít nhất (A += v)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Bổ sung giá trị xuất hiện ít nhất (A += v)

A += v

Source: AtCoder

Version: Phuoc Hung OJ Extended

Problem Statement

Initially, you have a sequence A=(A1,A2,…,An)A=(A_1,A_2,\ldots,A_n) of nn integers from 11 through mm, with positions numbered from 11. Imagine extending this sequence by performing 1010010^{100} append operations.

Before each append, consider all candidates v∈{1,2,…,m}v\in\{1,2,\ldots,m\}, including values not yet appearing in the sequence. Choose a value having the smallest current frequency. If several values tie, choose the smallest numeric value among them, then append it. Frequencies are updated before the next append.

Each of the qq independent queries specifies a 1-based position XX in the sequence after all 1010010^{100} operations. Output the value at that position. Queries refer to the same extended sequence; they do not trigger additional appends.

Input

  • Line 1: n m gives initial length and number of candidate values.
  • Line 2: A_1 A_2 ... A_n gives the initial values in index order.
  • Line 3: q, the number of position queries.
  • Final qq lines: each contains one 1-based index X, in query order.

Output

Print exactly qq lines. Line jj contains the element at position XjX_j after 1010010^{100} appends.

Subtasks

Subtask 1 (20 points):

  • 1≤n,m≤241\le n,m\le 24.
  • 1≤q≤301\le q\le 30.
  • 1≤Ai≤m1\le A_i\le m for all 1≤i≤n1\le i\le n.
  • 1≤Xj≤10181\le X_j\le 10^{18} for all 1≤j≤q1\le j\le q.

Subtask 2 (30 points):

  • 1≤n,m≤12001\le n,m\le 1200.
  • 1≤q≤16001\le q\le 1600.
  • 1≤Ai≤m1\le A_i\le m for all 1≤i≤n1\le i\le n.
  • 1≤Xj≤10181\le X_j\le 10^{18} for all 1≤j≤q1\le j\le q.

Subtask 3 (50 points):

  • 1≤n,m≤5000001\le n,m\le 500000.
  • 1≤q≤2000001\le q\le 200000.
  • 1≤Ai≤m1\le A_i\le m for all 1≤i≤n1\le i\le n.
  • 1≤Xj≤10181\le X_j\le 10^{18} for all 1≤j≤q1\le j\le q.

Examples

Input

3 3
1 1 2
8
1
2
3
4
5
6
7
8

Output

1
1
2
3
2
3
1
2

Explanation

Initially n=3n=3, m=3m=3 and the sequence is [1,1,2]. The starting frequencies of values 1,2,31,2,3 are respectively 2,1,02,1,0. Each append selects a value with minimum frequency, breaking ties toward the smaller value.

  • Positions 1, 2, 3: these belong to the initial sequence and are 1,1,21,1,2.
  • Position 4: value 33 has frequency 00, lower than the others; append 33.
  • Position 5: frequencies are now (2,1,1)(2,1,1). Values 22 and 33 tie for minimum frequency, so append the smaller value 22.
  • Position 6: frequencies become (2,2,1)(2,2,1); append 33.
  • Position 7: all three values now occur twice; append the smallest, 11.
  • Position 8: frequencies are (3,2,2)(3,2,2); choose 22, the smaller of the least-frequent values.

The first eight elements of the extended sequence are [1, 1, 2, 3, 2, 3, 1, 2]. Queries request positions 1 through 8, so each of these values is printed on its own line.