#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#define MAX long(26000000)
std::vector<std::vector<long>> dp;
struct Currency
{
Currency(int v, int w) : value(v), weight(w) { };
int weight;
int value;
};
class PBank
{
public:
bool init_pb_prop();
bool init_currencies();
bool evaluate();
private:
int empty_pb, full_pb;
std::vector<Currency> currencies;
};
int main()
{
int n_tests;
std::vector<PBank> vpb;
//std::cout << "Number of tests: ";
std::cin >> n_tests;
vpb.resize(n_tests);
for (auto &pb : vpb)
{
if (pb.init_pb_prop() && pb.init_currencies() && pb.evaluate())
continue;
else
std::cout << "This is impossible.\n";
}
return 0;
}
bool PBank::init_pb_prop()
{
//std::cout << "\nEmpty and full pb weight: ";
std::cin >> empty_pb >> full_pb;
if (empty_pb < 1 || empty_pb >= full_pb || full_pb > 10000)
return false;
else
return true;
}
bool PBank::init_currencies()
{
int curr_container_size, curr_val, curr_weight;
//std::cout << "\nCurrencies number: ";
std::cin >> curr_container_size;
if (curr_container_size < 1 || curr_container_size > 500)
return false;
for (int j = 0; j < curr_container_size; j++)
{
//std::cout << "\nCurrency nr " << j + 1 << ": ";
std::cin >> curr_val >> curr_weight;
if (curr_val < 1 || curr_val > 50000 || curr_weight < 1 || curr_weight > 10000)
return false;
else
currencies.push_back(Currency(curr_val, curr_weight));
}
std::sort(currencies.begin(), currencies.end(),
[](const Currency &c1, const Currency &c2)
{
return (static_cast<double>(c1.value) / c1.weight) > (static_cast<double>(c2.value) / c2.weight);
});
return true;
}
bool PBank::evaluate()
{
dp.resize(currencies.size(), std::vector<long>(full_pb - empty_pb + 1));
int div;
for (int wt = 0; wt <= full_pb - empty_pb; wt++)
{
for (auto i = 0; i < currencies.size(); i++)
{
if (wt == 0)
{
dp[i][wt] = 0;
continue;
}
div = wt / currencies[i].weight;
if (i == 0)
{
if (wt % currencies[i].weight == 0)
dp[i][wt] = div * currencies[i].value;
else
dp[i][wt] = MAX;
continue;
}
if (wt - currencies[i].weight < 0)
dp[i][wt] = std::min(dp[i - 1][wt], MAX);
else
{
if (div > 1 && wt >= currencies[i].weight * div)
dp[i][wt] = std::min(dp[i - 1][wt], dp[i][wt - currencies[i].weight * div] + currencies[i].value * div);
else
dp[i][wt] = std::min(dp[i - 1][wt], dp[i][wt - currencies[i].weight] + currencies[i].value);
dp[i][wt] = std::min(dp[i][wt], dp[i - 1][wt - currencies[i].weight] + currencies[i].value);
}
}
}
/*
for (int c = 0; c < dp.size(); c++)
{
for (int r = 0; r < dp[c].size(); r++)
std::cout << dp[c][r] << "\t";
std::cout << std::endl;
}
*/
int last = dp[currencies.size() - 1][full_pb - empty_pb];
if (last == MAX)
return false;
else
std::cout << "The minimum amount of money in the piggy-bank is " << last << ".\n";
return true;
}