#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  if (b >= a) {
    cout << b << endl;
    return;
  }
  if (c <= d) {
    cout << -1 << endl;
    return;
  }
  cout << b + (a - b + c - d - 1) / (c - d) * c << endl;
}
int main() {
  int t;
  cin >> t;
  while (t--) solve();
}
