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