#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define fi first #define se second #define pb push_back #define all(x) (x).begin(), (x).end() #define sz(x) (int((x).size())) #define bit(x) (1 << (x)) #define cnt1(x) (__builtin_popcount(x)) template inline void chkmax(T& x, T y) { if (x < y) x = y; } template inline void chkmin(T& x, T y) { if (y < x) x = y; } typedef long long LL; typedef double DB; typedef pair PII; typedef vector VI; int main() { int tc, n, m, p, q, val; for (scanf("%d", &tc); tc--; ) { scanf("%d%d%d%d", &n, &m, &p, &q); val = p * n; chkmin(val, q * (n / m) + p * (n % m)); chkmin(val, q * (n / m + 1)); printf("%d\n", val); } return 0; }