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