#include <bits/stdc++.h>
using namespace std;
void solve() {
  unsigned long long a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a > b && d >= c)
    cout << "-1"
         << "\n";
  else if (a <= b)
    cout << b << "\n";
  if (a > b && d < c) {
    if ((a - b) % (c - d) == 0)
      cout << b + (a - b) / (c - d) * c << "\n";
    else
      cout << b + ((a - b) / (c - d) + 1) * c << "\n";
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  unsigned long long t;
  cin >> t;
  while (t-- > 0) {
    solve();
  };
  return 0;
}
