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