#include <bits/stdc++.h>
using namespace std;
int T;
long long a, b, c, d, A, B, C, x, ans;
int main() {
  scanf("%d", &T);
  while (T--) {
    scanf("%lld%lld%lld%lld", &a, &b, &c, &d);
    if (a - b * c > 0) {
      puts("-1");
      continue;
    }
    ans = 0;
    A = -b * d;
    B = 2 * a - b * d;
    C = 2 * a;
    x = -B / double(2 * A);
    for (long long i = x - 2; i <= x + 2; i++) {
      long long y = i;
      y = max(y, 0LL);
      y = min(y, c / d);
      ans = max(ans, A * y * y + B * y + C);
    }
    printf("%lld\n", ans / 2);
  }
  return 0;
}
