#P3095. B Class Shuffle

    ID: 16353 Type: Default 1000ms 256MiB

B Class Shuffle

B Class Shuffle

Bessie is practicing a special card trick called the B Class Shuffle. She starts with a deck of N cards numbered from 1 to N (with 1 on top and N on the bottom) and a permutation p of M cards. The A class shuffle is defined as follows: given M cards (where \(2 \le M \le 10^5\)) arranged from top to bottom as \(1,2,\dots,M\), the card in the i-th position moves to position \(p_i\) after the shuffle. For example, if M = 3 and \(p = \{3,1,2\}\), then one A class shuffle transforms the order from [1,2,3] to [2,3,1] because card 1 moves to position 3, card 2 to position 1, and card 3 to position 2.

In the B class shuffle, Bessie uses the following process with a deck of N cards (M \le N \le 10^5):

  1. If the current deck has at least M cards, take the top M cards and perform one A class shuffle on them (i.e. the i-th card goes to the \(p_i\)-th position). Then, place the shuffled cards back on top of the deck.
  2. Remove the top card from the deck and place it onto a temporary stack. (When placing the card on the temporary stack, it is put on top of the existing cards.)
  3. Repeat steps 1 and 2 until the deck becomes empty. If at any point the deck has fewer than M cards, simply remove the top card (without shuffling) and place it on the temporary stack.

The temporary stack is built by placing each removed card on top, so the card removed last ends up at the very top. Given the initial deck in sorted order (1 on top, 2 next, …, N at the bottom), the permutation p (defining the A class shuffle), and Q queries asking for the card at a specified position in the temporary stack (with the top card at position 1), your task is to determine which card ends up at each query position after one complete B class shuffle.

Note on formulas: Use LaTeX format for any mathematical formulas. For example, use \(p = \{p_1,p_2,\dots,p_M\}\) and constraints \(2 \le M \le 10^5\) and \(M \le N \le 10^5\).

inputFormat

The first line contains three integers: N, M, and Q (M \le N \le 10^5 and \(1 \le Q \le \min(N,5000)\)).

The second line contains M integers: \(p_1, p_2, \dots, p_M\) (a permutation of \(\{1,2,\dots,M\}\), defining the A class shuffle).

Then follow Q lines, each containing one integer \(q_i\) (\(1 \le q_i \le N\)), which is a query asking for the card at position \(q_i\) in the temporary stack (with the top card counted as position 1).

outputFormat

Output Q lines. The \(i\)-th line should contain a single integer: the card number appearing at the \(q_i\)-th position in the temporary stack after performing the B class shuffle.

sample

6 3 3
3 1 2
1
2
3
5

6 4

</p>