Card Shuffle (Small)
InterviewTime limit5sMemory limit512 MB
Simulate C segment-to-top cuts on a deck of M ordered cards and report the card at position W.
- Level
Easy2 of 10
- Topics
- Simulation, Array
- Solved
- No attempts yet
Problem
Frank likes card games and spends his weekends at a friend's house playing them. The deck they use has cards, each carrying a different number from 1 to . Frank owns the same automatic card shuffling machine his friend uses, so he knows how it works. The machine shuffles the deck by cutting it times. The -th cut takes cards starting at position from the top, that is, the cards at positions through , and moves them to the top of the deck keeping their order.
One day the old deck got dirty, so a new one came out. The new deck goes into the shuffling machine exactly as it comes, with the cards ordered 1 to from the top. Knowing how the machine behaves, Frank wants to work out which card sits at position from the top after the shuffle.
Input
The first line holds a positive integer , the number of test cases. Each test case then follows in this format.
M C W
A1 B1
...
AC BC
The first line holds three integers , , separated by single spaces, where is the number of cards, is the number of cuts, and is the position you are asked about. Each of the next lines holds two integers , separated by a single space, meaning that the -th cut moves the cards starting at position from the top to the top of the deck.
Constraints
Output
For each test case, print one line in this form.
Case #X: P
is the test case number starting from 1, and is the card at position from the top of the deck after the shuffle.