#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long t;
  cin >> t;
  while (t--) {
    long long q, w, e, r;
    cin >> q >> w >> e >> r;
    if (e <= r && q > w)
      cout << -1 << endl;
    else if (q <= w)
      cout << w << endl;
    else {
      long long time = w + ((q - w) / (e - r)) * e;
      if ((q - w) % (e - r)) time += e;
      cout << time << endl;
    }
  }
  return 0;
}
