//
// main.cpp
// Richard
//
// Created by 邵金杰 on 16/9/9.
// Copyright © 2016年 邵金杰. All rights reserved.
//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=500+10;
const int inf=99999999;
int w[maxn],p[maxn],f[10000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int E,F;
scanf("%d%d",&E,&F);
int weigh=F-E;
for(int i=1;i<=weigh;i++) f[i]=inf;
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d%d",&p[i],&w[i]);
f[0]=0;
for(int i=0;i<n;i++)
{
for(int j=w[i];j<=weigh;j++)
{
f[j]=min(f[j],f[j-w[i]]+p[i]);
}
}
if(f[weigh]==inf) cout<<"This is impossible."<<endl;
else cout<<"The minimum amount of money in the piggy-bank is "<<f[weigh]<<"."<<endl;
}
return 0;
}
状态转移f[j]=min(f[j],f[j-w[i]]+p[i])。内层循环为递增。