#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 (b < a && c <= d)
      cout << "-1\n";
    else {
      long long n;
      if ((a - b) % (c - d) == 0)
        n = ceil((a - b) / (c - d));
      else
        n = (a - b) / (c - d) + 1;
      cout << (b + (n * c)) << "\n";
    }
  }
}
