Under construction

Still being built. Things may change

Last Hit

Time limit10sMemory limit512 MB

Summary
Choose which monsters to last-hit for gold while a tower shoots the closest living monster on its own turns.
Level

Medium6 of 10

Topics
Dynamic programming, Math
Solved
No attempts yet

Problem

Diana wants to earn as much gold as she can in her favorite game. She is standing next to her own tower, facing NN monsters. Diana and the tower take turns attacking the monsters, and Diana attacks first. On her turn Diana may pick one living monster and shoot it, and she may also do nothing and pass the turn. On its turn the tower shoots the living monster closest to it. Neither Diana nor the tower attacks a dead monster.

A shot from Diana lowers the monster's hit points by PP, and a shot from the tower lowers them by QQ. A monster whose hit points drop below 11 dies. Monster ii starts with HiH_i hit points. If Diana's shot kills monster ii, she receives GiG_i gold. If the tower's shot kills it, she receives nothing. Find the largest amount of gold Diana can obtain.

Input

The first line contains the number of test cases TT. The first line of each test case contains PP, QQ and NN, separated by spaces. The ii-th of the next NN lines contains HiH_i and GiG_i, separated by a space.

The monsters are listed in order of their distance from the tower. The tower shoots monster ii only after every monster with a smaller number is dead.

Limits

  • 1≤T≤1001 \le T \le 100
  • 20≤P≤20020 \le P \le 200
  • 20≤Q≤20020 \le Q \le 200
  • 1≤N≤1001 \le N \le 100
  • 1≤Hi≤2001 \le H_i \le 200
  • 0≤Gi≤1060 \le G_i \le 10^6

Output

For each test case, print one line in the format Case #x: y, where xx is the test case number starting from 11 and yy is the largest amount of gold Diana can obtain.

Hint

In the sample test case with P=20P = 20 and Q=60Q = 60, Diana should give up the first monster. If she spends her first two turns on the third monster and brings it down to 8080 hit points, she takes the last hit on both the second and the third monster.

Examples2

  1. Example 1

    Input
    2
    20 40 3
    100 100
    20 100
    60 100
    20 60 3
    80 100
    80 200
    120 300
    
    Expected output
    Case #1: 300
    Case #2: 500
    
  2. Example 2

    Input
    2
    200 20 1
    200 7
    20 200 1
    200 7
    
    Expected output
    Case #1: 7
    Case #2: 0