#include #include #include #include #include #define N 200005 #define INF 0x4fffffff #define PI 3.14159265358979323846 #define MM 1000000007 #define rep(i,j,k) for(i=j;i<=k;i++) #define rrep(i,j,k) for(i=j;i>=k;i--) using namespace std; int T,n,m,p,q,s,t; int ans; int main(){ cin>>T; while(T--){ scanf("%d%d%d%d",&n,&m,&p,&q); ans=p*n; t=n/m; s=n%m; ans=min(ans,t*q+s*p); ans=min(ans,(t+1)*q); printf("%d\n",ans); } return 0; }