#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  scanf("%d", &t);
  while (t--) {
    long long a, b, c, d;
    scanf("%lld %lld %lld %lld", &a, &b, &c, &d);
    if (b >= a)
      cout << b << endl;
    else {
      if ((c - d) <= 0)
        cout << "-1\n";
      else {
        long long temp = ((a - b) + (c - d - 1)) / (c - d);
        cout << b + temp * c << endl;
      }
    }
  }
  return 0;
}
