#include <bits/stdc++.h>
using namespace std;
const long long N = 1e6 + 10;
vector<long long> occ(N);
long long n, t, a, b, c, d, k, s;
int32_t main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  cin >> t;
  while (t--) {
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << "\n";
      ;
    } else {
      if (c <= d)
        cout << -1 << "\n";
      else {
        long long ans = 0;
        if ((a - b) % (c - d) == 0)
          ans = b + ((a - b) / (c - d)) * c;
        else
          ans = b + ((a - b) / (c - d) + 1) * c;
        cout << ans << "\n";
      }
    }
  }
  return 0;
}
