#include <bits/stdc++.h>
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template <class c>
struct rge {
  c b, e;
};
template <class c>
rge<c> range(c i, c j) {
  return rge<c>{i, j};
}
template <class c>
auto dud(c* x) -> decltype(cerr << *x, 0);
template <class c>
char dud(...);
struct debug {
  template <class c>
  debug& operator<<(const c&) {
    return *this;
  }
};
long long a, b, c, d;
long long S(long long x) {
  if (x < 0) return 0LL;
  return (x * (x + 1) / 2 + (d - 1) * (x - 1) * x / 2 + (c - 1) * x + x);
}
long long get(int x) {
  long long res = x * a;
  long long T = (x - 1) * d + 1;
  long long bf = (T - 2 + d) / d;
  ;
  long long af = max(0LL, (T - c - 2 + d) / d) + 1;
  bf = max(bf, 0LL);
  af = max(af, 0LL);
  debug() << " ["
          << "res"
             ": "
          << (res) << "] ";
  res -= c * b * max(0LL, af - 1);
  debug() << " ["
          << "res"
             ": "
          << (res) << "] ";
  debug() << " ["
          << "af"
             ": "
          << (af)
          << "] "
             " ["
          << "bf"
             ": "
          << (bf) << "] ";
  long long sum = S(bf) - S(af - 1);
  debug() << " ["
          << "sum"
             ": "
          << (sum)
          << "] "
             " ["
          << "T"
             ": "
          << (T)
          << "] "
             " ["
          << "S(bf)"
             ": "
          << (S(bf))
          << "] "
             " ["
          << "S(af - 1)"
             ": "
          << (S(af - 1)) << "] ";
  long long outside = sum - (T) * (bf - af + 1);
  if (outside >= 0) {
    sum = (bf - af + 1) * c - outside;
    res -= (sum)*b;
  }
  debug() << " ["
          << "sum"
             ": "
          << (sum)
          << "] "
             " ["
          << "outside"
             ": "
          << (outside) << "] ";
  return res;
}
void solve() {
  cin >> a >> b >> c >> d;
  long long lo = 1, hi = 2e6, ans = 0;
  for (int it = 0; it < 63; it++) {
    long long md1 = lo + (hi - lo) / 3;
    long long md2 = hi - (hi - lo) / 3;
    if (get(md1) > get(ans)) ans = md1;
    if (get(md2) > get(ans)) ans = md2;
    if (get(md1) >= get(md2)) {
      hi = md2;
    } else {
      lo = md1;
    }
  }
  if (get(ans) < get(ans + 1)) {
    cout << -1 << '\n';
  } else {
    assert(get(ans) >= 0);
    cout << get(ans) << '\n';
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    solve();
  }
}
