Quality Food (Large)

Time limit5sMemory limit512 MB

Summary
Given a budget, a per-delivery fee, and meal prices with shelf lives, find the most consecutive days of one meal a day starting from the first delivery.
Level

Medium7 of 10

Topics
Binary search, Greedy, Sorting
Solved
No attempts yet

Problem

You just moved from your hometown to a big city. You like everything about the new place except the food. The restaurants back home serve the best food in the region, called quality food, and you already miss it.

The largest restaurant in your hometown delivers. You can buy any amount of food in one delivery, and every delivery costs the same delivery fee FF no matter how much food it carries.

The restaurant serves NN types of food. Type ii has a price per meal PiP_i and a time to stale SiS_i. One meal feeds you for one day, and a meal that has been eaten cannot be eaten again. The time to stale is the number of days, counted from the day you receive the food, during which that food can still be eaten. If a delivery arrives on day dd, a meal of type ii from it can be eaten on any day from dd to d+Sid + S_i. A time to stale of 00 means you must eat that food on the day it arrives.

In a single delivery you can buy as many different types, and as many meals of each type, as you have money for. Buying more than Si+1S_i + 1 meals of type ii in one delivery makes no sense: at least one of those meals goes stale before you can eat it.

Delivery is very fast, so everything in one delivery arrives on the day you order it, and you may eat some of it that same day. Delivery is the only way for you to get quality food.

You have MM money to spend on meal prices and delivery fees. Find the largest number of days, counted from the day of your first delivery, on which you can eat quality food every single day.

Input

The first line of input gives the number of test cases TT. Then TT test cases follow. The first line of each test case has three integers MM, FF and NN: the amount of money you have, the delivery fee, and the number of types of food the restaurant serves. The next NN lines each hold two integers PiP_i and SiS_i, the price per meal and the time to stale of one type of food.

Limits

  • 1≤T≤501 \le T \le 50
  • 1≤F≤M≤10181 \le F \le M \le 10^{18}
  • 1≤N≤2001 \le N \le 200
  • 1≤Pi≤M1 \le P_i \le M
  • 0≤Si≤10180 \le S_i \le 10^{18}

Output

For each test case, print one line containing "Case #x: y", where xx is the test case number starting from 1 and yy is the maximum number of days on which you can eat at least one meal of quality food every day.

Note

Here is one way to reach three days in the first test case of the sample. On your first day in the city, order one meal of the first type and one meal of the second type, which costs 20 including the fee. Eat the first type that day and the second type the next day. On the third day, order one meal of the first type again and eat it the same day.

Examples1

  1. Example 1

    Input
    3
    32 5 2
    5 0
    10 2
    10 10 1
    10 10
    10 1 1
    1 5
    
    Expected output
    Case #1: 3
    Case #2: 0
    Case #3: 8