#define PROB Training #include #include #define f(x,y,z) for(int x = (y),__ = (z);x < __;++x) #define g(x,y,z) for(int x = (y),__ = (z);x <= __;++x) #define gd(x,y,z) for(int x = (y),__ = (z);x >= __;--x) #define CLR(X) memset(X,0,sizeof(X)) #define SET(ARR,X) memset(ARR,X,sizeof(ARR)) #define R(X,Y) f(X,0,Y) #define G(x,y) g(X,0,Y) #define MAXN 1024 #ifdef WIN32 #define LLD "%I64d" #define LLU "%I64u" #else #define LLD "%lld" #define LLU "%llu" #endif typedef long long LL; typedef long double real; int t; int n,m,p,q,sz; int main(){ scanf("%d",&t); while (t--){ scanf("%d%d%d%d",&n,&m,&p,&q); if (p * m <= q){ printf("%d\n",n * p); }else { sz = n / m * q; if (n % m * p < q)sz += n % m * p; else sz += q; printf("%d\n",sz); // printf("%d\n",(n / m) * q + (n % m) * p); } } return 0; }