#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a <= b) {
    cout << b << endl;
    return;
  }
  long long baki = (a - b);
  if (c <= d) {
    cout << -1 << endl;
    return;
  }
  long long ghm = (c - d);
  long long n = (baki / ghm);
  if (baki % ghm) n++;
  cout << (n * c) + b << endl;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int t = 1;
  cin >> t;
  while (t--) solve();
  return 0;
}
