#include <bits/stdc++.h>
using namespace std;
int main(int argc, char *argv[]) {
  long long n, i, j, k, l, m, t, p, a, b, c, d;
  cin >> t;
  while (t--) {
    cin >> a >> b >> c >> d;
    if (b >= a)
      cout << b << endl;
    else if (c <= d)
      cout << "-1\n";
    else {
      j = c - d;
      i = a - b;
      k = (i / j);
      if (i % j != 0) k++;
      cout << b + (c * k) << endl;
    }
  }
  return 0;
}
