This page is still under construction.

Parts of this page are still being built. What you see may change.

The Bessie Shuffle

Time limit1sMemory limit128 MB

Summary
Shuffle the top M cards with the given permutation, move the top card to a pile, repeat until empty, and report the card at each queried pile position.
Level

Medium7 of 10

Topics
Simulation, Math
Solved
No attempts yet

Problem

Bessie is practicing her card tricks. She has already mastered the Bessie shuffle, a shuffle on MM cards (2≤M≤100,0002 \le M \le 100{,}000) that rearranges them so the ii-th card from the top becomes the PiP_i-th card from the top.

Now Bessie practices on a bigger deck. It holds NN cards (M≤N≤1,000,000,000M \le N \le 1{,}000{,}000{,}000) labeled 11 to NN. She takes the top MM cards, performs the Bessie shuffle on them, and puts them back on top of the deck. She then removes the top card and places it face down. She repeats this, stacking each removed card on top of the ones she has already placed, until the deck is empty. Once fewer than MM cards are left she stops shuffling, but she keeps moving the top card onto the pile.

The deck starts sorted, with 11 on top, 22 next and NN on the bottom. Given the Bessie shuffle, answer QQ queries (1≤Q≤N1 \le Q \le N, Q≤5,000Q \le 5{,}000): which card ends up at position qiq_i from the top of the finished pile?

Input

The first line contains NN, MM and QQ separated by spaces.

Each of the next MM lines contains one integer. Line i+1i+1 holds PiP_i, the position that the ii-th card from the top moves to in the Bessie shuffle (1≤Pi≤M1 \le P_i \le M). The values P1P_1 through PMP_M use each of 11 through MM exactly once.

Each of the next QQ lines contains one integer qiq_i, the ii-th query (1≤qi≤N1 \le q_i \le N). It asks for the card at position qiq_i from the top of the finished pile.

Output

Print QQ lines. On line ii, print the label of the card at position qiq_i from the top of the finished pile.

Hint

In the first example Bessie has 55 cards ordered [1,2,3,4,5][1, 2, 3, 4, 5]. Her shuffle works on 33 cards and moves the top card of the window to the bottom. The deck changes like this:

  • [1,2,3,4,5]→[2,3,1,4,5][1, 2, 3, 4, 5] \to [2, 3, 1, 4, 5], then 22 goes face down
  • [3,1,4,5]→[1,4,3,5][3, 1, 4, 5] \to [1, 4, 3, 5], then 11 goes face down
  • [4,3,5]→[3,5,4][4, 3, 5] \to [3, 5, 4], then 33 goes face down
  • [5,4][5, 4], then 55 goes face down
  • [4][4], then 44 goes face down

The finished pile reads [4,5,3,1,2][4, 5, 3, 1, 2] from the top.

Examples1

  1. Example 1

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