#include <bits/stdc++.h>
const int INF = 1 << 30;
const long long LINF = 1ll << 62;
const unsigned long long ULINF = 1ull << 63;
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  int tt;
  cin >> tt;
  while (tt--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a <= b) {
      cout << b << endl;
    } else {
      if (d >= c) {
        cout << -1 << endl;
      } else {
        long long t = c - d;
        t = (a - b) / (c - d);
        if ((a - b) % (c - d)) t++;
        cout << b + t * c << endl;
      }
    }
  }
}
