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