#include <bits/stdc++.h>
using namespace std;
inline bool EQ(double a, double b) { return fabs(a - b) < 1e-9; }
const int inf = 1 << 29;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    a -= b;
    if (a <= 0)
      cout << b << endl;
    else {
      if (c <= d) {
        cout << -1 << endl;
      } else {
        long long ss = c - d;
        long long nss = a / ss;
        if (a % ss != 0) nss++;
        cout << b + nss * c << endl;
      }
    }
  }
  return 0;
}
