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