#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a)
      cout << b << "\n";
    else if (c <= d)
      cout << -1 << "\n";
    else {
      long long cont = 0, aux = b;
      int ans = a - b;
      cont = ans / (c - d);
      b += ((c - d) * cont);
      while (a > b) {
        b += (c - d);
        cont++;
      }
      cout << aux + (cont * c) << "\n";
    }
  }
}
