20pts:
#include <bits/stdc++.h>
using namespace std;
int main(){
freopen("guru.in","r",stdin);
freopen("guru.out","w",stdout);
int t;
cin>>t;
while(t--){
int n,a,b,x,y,z;
cin>>n>>a>>b>>x>>y>>z;
y=min(a*x,y);
z=min(z,b*x);
if(a*z<b*y){
swap(a,b),swap(y,z);
}
if(a*x==y&&b*x==z){
cout<<n*x<<endl;
}else{
int sum=0;
if(a*x>y&&b*x>z){
if(n/a>a){
sum=n/b*z+(n%b*x);
for(int i=0;i<a;i++){
if(i*b>n) break;
int m=n;
m-=b*i;
int res=i*z;
res+=m/a*y+(m%a)*x;
sum=min(sum,res);
}
}else{
sum=n/a*y+(n%a)*x;
for(int i=0;i*a<=n;i++){
if(i*a>n) break;
int m=n;
m-=a*i;
int res=i*y;
res+=m/b*z+(m%b)*x;
sum=min(sum,res);
}
}
}else if(a*x>y){
sum=n/a*y+(n%a)*x;
}else if(b*x>z){
sum=n/b*z+(n%b)*x;
}
cout<<sum<<endl;
}
}
}