# 【uva】12325 - Zombie's Treasure Chest

 宝藏一 宝藏二 单位体积 S1 S2 单位价格 V1 V2 数量 S2 S1 总价格 S2*V1 S1*V2 总体积 S1*S2 S1*S2

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<stack>
#include<queue>
#include<map>
#include<vector>
typedef long long LL;
using namespace std;
LL n,V1,S1,V2,S2;
#define INF 5000
LL solve(int choice){
LL MAX = -1;
if(choice == 0){
for(LL i = 0 ; i * S1 <= n ; i++){
LL temp = n - i * S1;
LL ans  = temp / S2 * V2 + i * V1;
MAX = max(MAX,ans);
}
}
else if(choice == 1){
for(LL i = 0 ; i * S2 <= n ; i++){
LL temp = n - i * S2;
LL ans  = temp / S1 * V1 + i * V2;
MAX = max(MAX,ans);
}
}
else {
LL Sum1 = S2 * V1;
LL Sum2 = S1 * V2;
if(Sum1 > Sum2){
for(LL i = 0 ; i < S1 ; i++){
LL temp = n - i * S2;
LL ans  = temp / S1 * V1 + i * V2;
MAX = max(MAX,ans);
}
}
else {
for(LL i = 0 ; i < S2 ; i++){
LL temp = n - i * S1;
LL ans  = temp / S2 * V2 + i * V1;
MAX = max(MAX,ans);
}
}
}
return MAX;
}
int main(){
int T,Case = 1;
scanf("%d",&T);
while(T--){
scanf("%lld%lld%lld%lld%lld",&n,&S1,&V1,&S2,&V2);
/*分别输入宝物一和宝物二的体积 价值*/
LL ans;
if((n / S1) < INF)
ans = solve(0);
else if((n / S2) < INF)
ans = solve(1);
else {
ans = solve(2);
}
printf("Case #%d: %lld\n",Case ++, ans);
}
return 0;
}