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