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