#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a <= b) {
      cout << b << "\n";
    } else {
      if (c > d) {
        a -= b;
        int f = a / (c - d);
        if (a % (c - d) != 0) {
          f++;
        }
        long long ans = b + f * c;
        cout << ans << "\n";
      } else {
        cout << "-1\n";
      }
    }
  }
  return 0;
}
