简单dp
#include <iostream>
using namespace std;
int dp[10005];
int main()
{
int v[505];
int w[505];
int e,f;
int T;
cin>>T;
while (T--)
{
cin>>e>>f;
memset(v,0,sizeof(v));
memset(w,0,sizeof(w));
memset(dp,0,sizeof(dp));
int n;
cin>>n;
for (int i=1;i<=n;i++)
cin>>v[i]>>w[i];
int sum=f-e;
for (int i=1;i<=sum;i++)
{
for (int t=1;t<=n;t++)
{
if ((i>=w[t]&&dp[i]==0)||(i>=w[t]&&dp[i]>dp[i-w[t]]+v[t]))
{
if (dp[i-w[t]]!=0||i-w[t]==0)
dp[i]=dp[i-w[t]]+v[t];
}
}
}
/*for (int i=1;i<=sum;i++)
cout<<dp[i]<<" ";
cout<<endl;*/
if (dp[sum]!=0)
printf("The minimum amount of money in the piggy-bank is %d.\n",dp[sum]);
else
printf("This is impossible.\n");
}
}