#include <bits/stdc++.h>
using namespace std;
const int N = 1000004;
long long a, b, c, d;
long long que(long long x) {
  long long tm = (x - 1) * d;
  long long pre = tm - c, v = 0;
  if (pre >= 0) v = pre / d + 1;
  long long ans = a * x;
  ans -= v * c * b;
  x -= v + 1;
  ans -= (x * (x + 1) / 2) * d * b;
  return ans;
}
int main() {
  int T;
  scanf("%d", &T);
  while (T--) {
    scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
    int l = 1, r = N;
    while (l < r) {
      int mid = (l + r) >> 1;
      if (que(mid + 1) <= que(mid))
        r = mid;
      else
        l = mid + 1;
    }
    long long ans = l;
    if (l >= N)
      ans = -1;
    else {
      for (int i = max(0, l - 2); i <= l + 2; i++) ans = max(ans, que(i));
    }
    printf("%lld\n", ans);
  }
}
