#include<bits/stdc++.h>
using namespace std;


int main(){
  int n;
  cin>>n;
  int a[1000],b[1000],c[1000],d[1000],g[1000];

  for(int i=0;i<n;i++){
    cin>>a[i]>>b[i]>>c[i]>>d[i];
    g[i]=a[i]*c[i]+b[i]*d[i];
    int p,q;
    p=5-c[i];
    q=2-d[i];
    if(p<=0 && q<=0){g[i]=g[i]*4/5; continue;}
    if(p<0){p=0;}
    if(q<0){q=0;}
    g[i]=min(g[i],(g[i]+p*a[i]+q*b[i])*4/5);
  }


  for(int i=0;i<n;i++){
    cout<<g[i]<<endl;
  }
  
  return 0;
}

