#include<iostream>                                                                                                                                      
#include<algorithm>                                                                                                                                     
using namespace std;                                                                                                                                    
                                                                                                                                                        
int main()                                                                                                                                              
{                                                                                                                                                       
  int n;                                                                                                                                                
  for(;cin>>n;)                                                                                                                                         
    {                                                                                                                                                   
      for(int i=0;i<n;i++)                                                                                                                              
        {                                                                                                                                               
          int x,y,b,p;                                                                                                                                  
          cin>>x>>y>>b>>p;                                                                                                                              
          int ans;                                                                                                                                      
          if(b>4&&p>1)                                                                                                                                  
            ans=(int((x*b)+(y*p))*0.8);                                                                                                                 
          else if(b>4)                                                                                                                                  
            ans=min((int)(((x*b)+(y*2))*0.8),(x*b)+(y*p));                                                                                              
          else if(p>1)                                                                                                                                  
            ans=min((int)(((x*5)+(y*p))*0.8),(x*b)+(y*p));                                                                                              
          else                                                                                                                                          
            ans=min((int)(((x*5)+(y*2))*0.8),(x*b)+(y*p));                                                                                              
          cout<<ans<<endl;                                                                                                                              
        }                                                                                                                                               
    }                                                                                                                                                   
} 

Measured memory_bytes: 1160