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