#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;
    long long count;
    long long ans;
    if (b >= a)
      cout << b << endl;
    else {
      if (c <= d)
        cout << -1 << endl;
      else {
        if ((a - b) % (c - d) == 0)
          count = (a - b) / (c - d);
        else
          count = (a - b) / (c - d) + 1;
        ans = b + count * c;
        cout << ans << endl;
      }
    }
  }
  return 0;
}
