#P3095. [USACO13DEC] The Bessie Shuffle S

[USACO13DEC] The Bessie Shuffle S

Description

Bessie is practicing her card tricks. She has already mastered the Bessie-shuffle -- a shuffle on MM (2M100,0002 \le M \le 100,000) cards that reorganizes the cards so the i-th card from the top is now the pip_i-th card from the top.

Now Bessie is practicing shuffles on larger decks. She has a deck of NN cards (MN100,000M \le N \le 100,000) conveniently labeled 11 to NN. She shuffles this deck by taking the first MM cards and performing the Bessie-shuffle on them, placing the shuffled cards back on top of the deck.She then removes the top card from the deck and places it face down. She repeats this process, placing the top cards successively on top of each other, until she is out of cards. When Bessie has less than MM cards left, she no longer performs the Bessie-shuffle, but continues to place the top card on top of the others.

Bessie knows that the deck initially started in sorted order, with 11 on top, 22 next, and NN on the bottom. Given the description of the Bessie-shuffle, help Bessie compute which cards end up located at QQ different specified positions (1QN,Q5,0001 \le Q \le N, Q \le 5,000) in the deck.

Input Format

Line 11: A single line containing NN, MM and QQ separated by a space.

Lines 21+M2 \dots 1+M: Line i+1i+1 indicates the position from the top, pip_i, of the ii-th card in the Bessie-shuffle (1piM1 \le p_i \le M).

Lines 2+M1+M+Q2+M \dots 1+M+Q: Line i+1+Mi+1+M contains a single integer qiq_i.

describing the ii-th query.
You are to compute the label on the card located in position qiq_i from the top (1qiN1 \le q_i \le N).

Output Format

Lines 1Q1 \dots Q: On the ii-th line, print a single integer indicating the card at position qiq_i from the top.

5 3 5 
3 
1 
2 
1 
2 
3 
4 
5 

4 
5 
3 
1 
2 

Hint

Bessie has a deck of 55 cards initially ordered as [1,2,3,4,5][1, 2, 3, 4, 5]. Her shuffle is on 33 cards and has the effect of moving the top card to the bottom.
There are 55 queries querying each position in the deck.

The shuffle proceeds as:

[1, 2, 3, 4, 5] -> [2, 3, 1, 4, 5] (put 2 face down) 
[3, 1, 4, 5] -> [1, 4, 3, 5] (put 1 face down) 
[4, 3, 5] -> [3, 5, 4] (put 3 face down) 
[5, 4] (put 5 face down) 
[4] (put 4 face down) 

This produces the final order of [4,5,3,1,2][4, 5, 3, 1, 2].