#include <bits/stdc++.h>
#pragma GCC optimize("O3", "unroll-loops")
#pragma GCC target("sse4.2")
using namespace std;
mt19937 gen(chrono::high_resolution_clock::now().time_since_epoch().count());
void flush() { cout << flush; }
void flushln() { cout << endl; }
void println() { cout << '\n'; }
template <class T>
void print(const T &x) {
  cout << x;
}
template <class T>
void read(T &x) {
  cin >> x;
}
template <class T, class... U>
void read(T &x, U &...u) {
  read(x);
  read(u...);
}
template <class T, class... U>
void print(const T &x, const U &...u) {
  print(x);
  print(u...);
}
template <class T, class... U>
void println(const T &x, const U &...u) {
  print(x);
  println(u...);
}
template <class T>
inline void sort(T &a) {
  sort((a).begin(), (a).end());
}
template <class T>
inline void rsort(T &a) {
  sort((a).rbegin(), (a).rend());
}
template <class T>
inline void reverse(T &a) {
  reverse((a).begin(), (a).end());
}
template <class T, class U>
inline istream &operator>>(istream &str, pair<T, U> &p) {
  return str >> p.first >> p.second;
}
template <class T>
inline istream &operator>>(istream &str, vector<T> &a) {
  for (auto &i : a) str >> i;
  return str;
}
template <class T>
inline T sorted(T a) {
  sort(a);
  return a;
}
void solve() {
  long long a, b, c, d;
  read(a, b, c, d);
  long long l = 0, r = 1;
  auto first = [&](long long k) {
    long long t = k * d;
    long long l = max(0ll, (t - c) / d);
    long long r = (t / d + 1) - l;
    long long v1 = l * d;
    long long sum = r * t - (2 * v1 + d * (r - 1)) * r / 2;
    return l * (b * c - a) + r * (-a) + sum * b;
  };
  while (first(r) >= -1e18 && r < 2e6) r *= 2;
  if (first(r + 1) < first(r)) {
    println(-1);
    return;
  }
  while (r - l > 2) {
    long long m1 = l + (r - l) / 3, m2 = r - (r - l) / 3;
    if (first(m1) > first(m2))
      l = m1;
    else
      r = m2;
  }
  long long ans = l;
  for (long long i = l + 1; i <= r; ++i)
    if (first(i) < first(ans)) ans = i;
  println(-first(ans));
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << fixed << setprecision(12);
  int t;
  read(t);
  while (t--) solve();
  return 0;
}
