#include <bits/stdc++.h>
using namespace std;
template <typename G1, typename G2 = G1, typename G3 = G1>
struct triple {
  G1 first;
  G2 second;
  G3 T;
};
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a > b * c)
      cout << "-1\n";
    else if (c <= d)
      cout << a << '\n';
    else {
      auto val = [&](long long x) {
        long long k = max(0LL, (x * d - c) / d);
        return (long double)a * x - (long double)k * c * b -
               (long double)d * b * (x - k) * (x - k - 1) / 2;
      };
      long long l = 1, r = 1e12, ans = a;
      while (r - l > 10) {
        long long m1 = l + (r - l) / 3;
        long long m2 = l + 2 * (r - l) / 3;
        if (val(m1) <= val(m2))
          l = m1;
        else
          r = m2;
      }
      for (long long i = l; i <= r; i++)
        ans = max(ans, (long long)round(val(i)));
      cout << ans << '\n';
    }
  }
  return 0;
}
