#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (d >= c && b < a)
      cout << -1 << endl;
    else if (a <= b)
      cout << b << endl;
    else {
      int t;
      if ((a - b) % (c - d) == 0)
        t = 0;
      else
        t = 1;
      cout << b + c * ((a - b) / (c - d) + t) << endl;
    }
  }
  return 0;
}
