ACM Programming Contest
ACM International Collegiate Programming Contest
Sponsored by IBM 

Central Europe 1999 Regional Contest

Czech Technical University in Prague 
Dept. of Computer Science, Faculty of Electrical Engineering 

November 13, 1999 


Contest Problems and Introduction

Dear Contestants: 

We have prepared a set of very interesting real-life problems for you. A team called Archaeologists of the Central Mountains (ACM) is interested in the famous Helmut's Pyramid (HP) in Egypt. In the Pyramid, there is a Sarcophagus of Umbertiti Nefrites (SUN). According to the legend, the Sarcophagus contains (besides the pharaoh's body, of course) a priceless treasure: air-tickets to Orlando, FL, with the magic date March 2000. (Editor's note: the equivalent is Vancouver in 2001, and will perhaps be  Hawaii in 2002.) 


(file name:

Before ACM can get to Orlando, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from the Irreversibly Bound Money (IBM) corporation. The idea is simple. Whenever some ACM member has any change, she takes all the coins and deposits them into a designated piggy-bank. This process is irreversible, in that the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay for everything they need.

But there is a big problem with piggy-banks: it is not possible to determine how much money is inside. We could break the pig into pieces to do so, only to find out that there is not enough money -- and we'd have to spend some of it on a new bank! Clearly, we want to avoid this unpleasant situation. The ACM has decided instead to weigh the piggy-bank and try to guess how much money is inside. Assume that we are able to determine the weight of the pig exactly and that we know the exact weights of all the coins of a given currency. Then, given the weight of the bank, there is some minimum amount of money that we can guarantee is inside. Your task is to find out this minimum amount. We need your help to ensure there are no more prematurely broken pigs! 

Input Specification

The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig currently filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, so that 1 <= E <= F <= 10000. On the second line of each test case, there is an integer N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, V and W (1 <= V <= 50000, 1 <= W <=10000). V is the value of the coin in monetary units, W is it's weight in grams. 

Output Specification

Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.". 

Sample Input

10 110
1 1
30 50
10 110
1 1
50 30
1 6
10 3
20 4

Output for the Sample Input

The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.