This page is still under construction.

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

Card Shuffle (Small)

Interview

Time limit5sMemory limit512 MB

Summary
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 MM cards, each carrying a different number from 1 to MM. 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 CC times. The ii-th cut takes BiB_i cards starting at position AiA_i from the top, that is, the cards at positions AiA_i through Ai+Bi−1A_i + B_i - 1, 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 MM from the top. Knowing how the machine behaves, Frank wants to work out which card sits at position WW from the top after the shuffle.

Input

The first line holds a positive integer TT, 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 MM, CC, WW separated by single spaces, where MM is the number of cards, CC is the number of cuts, and WW is the position you are asked about. Each of the next CC lines holds two integers AiA_i, BiB_i separated by a single space, meaning that the ii-th cut moves the BiB_i cards starting at position AiA_i from the top to the top of the deck.

Constraints

  • 1≤T≤2001 \le T \le 200
  • 1≤C≤1001 \le C \le 100
  • 1≤W≤M1 \le W \le M
  • 1≤Ai≤M1 \le A_i \le M
  • 1≤Bi≤M1 \le B_i \le M
  • 1≤Ai+Bi−1≤M1 \le A_i + B_i - 1 \le M
  • 1≤M≤1001 \le M \le 100

Output

For each test case, print one line in this form.

Case #X: P

XX is the test case number starting from 1, and PP is the card at position WW from the top of the deck after the shuffle.

Examples3

  1. Example 1

    Input
    3
    1 1 1
    1 1
    2 3 1
    2 1
    2 1
    2 1
    5 3 2
    4 2
    5 1
    4 2
    
    Expected output
    Case #1: 1
    Case #2: 2
    Case #3: 2
    
  2. Example 2

    Input
    1
    1 1 1
    1 1
    
    Expected output
    Case #1: 1
    
  3. Example 3

    Input
    2
    100 1 100
    1 100
    100 1 1
    1 100
    
    Expected output
    Case #1: 100
    Case #2: 1