#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int t;
  cin >> t;
  while (t--) {
    long long int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << endl;
    } else {
      int remain = a - b;
      if (d >= c) {
        cout << "-1" << endl;
      } else {
        int div = remain / (c - d);
        if (remain % (c - d)) {
          div++;
        }
        cout << div * c + b << endl;
      }
    }
  }
  return 0;
}
