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
Repeatedly shuffle the top M cards by a fixed permutation, deal the top card onto a pile, and report the labels at queried pile positions.
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 that moves the ii-th card from the top to position P[i]P[i] from the top.

Now Bessie practices on a bigger deck. The deck holds NN cards labeled 11 through NN. She takes the top MM cards, performs the Bessie shuffle on them, and puts the shuffled cards back on top of the deck. She then removes the top card and places it face down. She repeats this until the deck runs out. Each card she removes goes on top of the cards she has already put down. Once fewer than MM cards are left she stops shuffling and only keeps moving the top card onto the pile.

The deck starts in sorted order with 11 on top, 22 next and NN at the bottom. Given the description of the Bessie shuffle, compute the label of the card that ends up at each of the positions q1,q2,…,qQq_1, q_2, \dots, q_Q of the finished pile, counted from its top.

Input

The first line contains NN, MM and QQ separated by spaces (2≤M≤N≤100,0002 \le M \le N \le 100{,}000, 1≤Q≤min⁡(N,5,000)1 \le Q \le \min(N, 5{,}000)).

Each of the next MM lines holds P[i]P[i], the position that the ii-th card from the top moves to during the Bessie shuffle (1≤P[i]≤M1 \le P[i] \le M). PP is a permutation of 11 through MM.

Each of the following QQ lines holds one query qiq_i (1≤qi≤N1 \le q_i \le N).

Output

Print QQ lines. Line ii holds the label of the card at position qiq_i from the top of the finished pile.

Hint

Take five cards stacked as [1,2,3,4,5][1, 2, 3, 4, 5] from the top, with a shuffle on three cards that sends the top card to the bottom. The process runs like this:

  • [1,2,3,4,5]→[2,3,1,4,5][1, 2, 3, 4, 5] \to [2, 3, 1, 4, 5], put 22 face down
  • [3,1,4,5]→[1,4,3,5][3, 1, 4, 5] \to [1, 4, 3, 5], put 11 face down
  • [4,3,5]→[3,5,4][4, 3, 5] \to [3, 5, 4], put 33 face down
  • [5,4][5, 4], fewer than three cards remain, so put 55 face down with no shuffle
  • [4][4], put 44 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