#include<iostream> #include<stdio.h> #include<algorithm> #include<string.h> using namespace std; struct node{ long long S; long long P; double q; }a[3]; bool cmp(node a,node b){ return a.q>b.q; } int main(){ int T; long long V; long long sum; long long dp[6000]; long long tmp; int i,j; int ca=0; scanf("%d",&T); while(T--){ scanf("%lld%lld",&a[0].S,&a[0].P); a[0].q=(double)(a[0].P)/a[0].S; //cout<<a[0].q<<endl; scanf("%lld%lld",&a[1].S,&a[1].P); a[1].q=(double)(a[1].P)/a[1].S; //cout<<a[1].q<<endl; scanf("%lld%lld",&a[2].S,&a[2].P); a[2].q=(double)(a[2].P)/a[2].S; //cout<<a[2].q<<endl; scanf("%lld",&V); sort(a,a+3,cmp); sum=V%a[0].S; for(i=1;;++i){ if(a[0].S*i>=5000){ break; } } sum=sum+a[0].S*i; memset(dp,0,sizeof(dp)); for(i=0;i<3;++i){ for(j=a[i].S;j<=sum;++j){ tmp=dp[j-a[i].S]+a[i].P; if(tmp>dp[j]){ dp[j]=tmp; } } } for(i=sum;;--i){ if(dp[i]>0){ break; } } printf("Case %d: %lld\n",++ca,((V-sum)/a[0].S)*a[0].P+dp[i]); } return 0; } /************************************** Problem id : SDUT OJ J User name : 666777 Result : Accepted Take Memory : 556K Take Time : 0MS Submit Time : 2016-04-30 17:44:39 **************************************/