Algorithm title: UVA 10280 old Wine into New bottles (DP full backpack)

Source: Internet
Author: User
Tags integer

Problem c:old Wine into New bottles

Wine bottles are never completely filled:a small amount to-is left in the must to neck for allow thermal D contraction. If too little the bottle, the wine may expand and expel the cork; If too much of the air is left in the bottle, the wine may spoil. Thus each bottle has a minimum and maximum capacity.

Given a certain amount of wine and a selection of of bottles of various sizes, determine which bottles to? Filled to between it minimum and maximum capacity and so, as much wine as possible.

Input

The input begins with a single positive integer in a line by itself indicating the number of cases following, each of them as described below. This are followed by a blank line, and there are also a blank line between two consecutive.

The "a" of input contains two integers:the amount of wine to is bottled (in litres, between 0 and 1,000,000) and T He number of sizes of bottles (between 1 and 100). For each size of bottle, one line of input follows giving the minimum and maximum capacity of each bottle in millilitres. The maximum capacity isn't less than 325 ml and does not exceed ML. The minimum capacity is not less than the greater than of the 99% maximum. You may assume this unlimited number of each bottle is available.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases is separated by a blank line.

Your output should consist of a single integer:the amount of wine, in ML, which cannot be bottled.

Sample Input

2

2
4450 4500
725 750

10000 2
4450 4500
725 750

Sample Output



0

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.