#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
void err(istream_iterator<string> it) { cout << '\n'; }
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << "[" << *it << " = " << a << "] ";
  err(++it, args...);
}
const long long inf = 2e9 + 7;
const long long INF = 3e18 + 7;
long long mod = 1e9 + 7;
long long MOD = 1e15 + 7;
const bool multiple_tests = 1;
const long long N = 5e5 + 7;
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a <= b) {
    cout << b;
    return;
  }
  a -= b;
  if (d >= c) {
    cout << -1;
    return;
  }
  long long del = c - d;
  long long e = ceil(a * 1.0 / del);
  cout << e * c + b;
}
signed main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long TT = 1;
  if (multiple_tests) cin >> TT;
  for (long long T = 1; T <= TT; T++) {
    solve();
    cout << '\n';
  }
  cerr << "\nTime elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n";
  return 0;
}
