Card Shuffle (Large)

Find the card at position W after moving the given blocks of a numbered M-card deck to the top C times.

Medium5SimulationIntervalsInterviewNo attempts yetTime limit5sMemory limit512 MB

Problem

Frank likes card games and spends his weekends at game parties in a friend's house. The deck they play with has MM cards, each carrying a different number from 11 to MM. Frank owns the same automatic shuffling machine his friend uses at those parties, and he knows how it works. The machine shuffles the deck by cutting the pile CC times. Cut ii takes BiB_i cards starting at the AiA_i-th card from the top, that is the cards at positions AiA_i through Ai+Bi1A_i + B_i - 1, and moves them to the top of the pile without changing their order.

One day the usual deck got dirty, so they opened a new one. The new deck went into the shuffling machine exactly as it came, stacked from 11 at the top down to MM. Frank wants to use what he knows about the machine to work out which card sits at position WW from the top once the shuffle is over.

Input

The first line holds the number of test cases TT. 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 a single space, where MM is the number of cards, CC is the number of cuts, and WW is the position Frank asks about. Each of the next CC lines holds two integers AiA_i, BiB_i separated by a single space, meaning that cut ii moves the BiB_i cards starting at the AiA_i-th card from the top to the top of the pile.

Constraints

  • 1T2001 \le T \le 200
  • 1C1001 \le C \le 100
  • 1WM1 \le W \le M
  • 1AiM1 \le A_i \le M
  • 1BiM1 \le B_i \le M
  • 1Ai+Bi1M1 \le A_i + B_i - 1 \le M
  • 1M1091 \le M \le 10^9

Output

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

Case #X: P

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