#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    long long int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a)
      cout << b << endl;
    else {
      long long int sum = b;
      if (d >= c) {
        cout << -1 << endl;
      } else {
        if ((a - b) % (c - d) != 0) sum += c;
        c *= ((a - b) / (c - d));
        sum += c;
        cout << sum << endl;
      }
    }
  }
}
