/*#include using namespace std; typedef pair P; char ans[50002]; string temp[115] = {"H","He","Li","Be","B","C","N","O","F","Ne","Na","Mg","Al","Si","P","S","Cl","Ar","K","Ca","Sc","Ti","V","Cr","Mn","Fe","Co","Ni","Cu","Zn","Ga","Ge","As","Se","Br","Kr", "Rb","Sr","Y","Zr","Nb","Mo","Tc","Ru","Rh","Pd","Ag","Cd","In","Sn","Sb","Te","I","Xe","Cs","Ba","Hf","Ta","W","Re","Os","Ir","Pt","Au","Hg","Tl","Pb","Bi","Po","At","Rn", "Fr","Ra","Rf","Db","Sg","Bh","Hs","Mt","Ds","Rg","Cn","Fl","Lv","La","Ce","Pr","Nd","Pm","Sm","Eu","Gd","Tb","Dy","Ho","Er","Tm","Yb","Lu", "Ac","Th","Pa","U","Np","Pu","Am","Cm","Bk","Cf","Es","Fm","Md","No","Lr"}; int main () { int T; scanf("%d",&T);getchar(); while (T--) { scanf("%s",ans); queue

q; string t = ""; t += ans[0]; if (find(temp,temp + 114,t) != (temp + 114))q.push(P(t,0)); t += ans[1]; if (find(temp,temp + 114,t) != (temp + 114))q.push(P(t,0)); bool flag = false; while (!q.empty()) { P p = q.front(); int index = p.second; if () string s = ""; if (p.first.size() == 1) { s += ans[index + 1]; if (find(temp,temp + 114,s) != (temp + 114))q.push(P(s,index + 1)); s += ans[index + 2]; if (find(temp,temp + 114,s) != (temp + 114))q.push(P(s,index + 1)); q.pop(); } else { s += ans[index + 2]; if (find(temp,temp + 114,s) != (temp + 114))q.push(P(s,index + 2)); s += ans[index + 3]; if (find(temp,temp + 114,s) != (temp + 114))q.push(P(s,index + 2)); q.pop(); } } } return 0; }*/ #include #include using namespace std; int main () { int T; scanf("%d",&T); while (T--) { int n,m,p,q; scanf("%d%d%d%d",&n,&m,&p,&q); if (m * p <= q)printf("%d\n",n * p); else { int zu = n / m,sh = n - zu * m; if (sh == 0)printf("%d\n",zu * q); else { if (p * sh >= q)printf("%d\n",(zu + 1) * q); else printf("%d\n",zu * q + sh * p); } } } return 0; }