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