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