#include <bits/stdc++.h>
using namespace std;
const int mxN = 2e5;
int n, a[mxN], k;
void solve() {
  long long a, b, c, d;
  cin >> a >> b >> c >> d;
  int x = a - b;
  if (x <= 0) {
    cout << b << endl;
    return;
  }
  int y = c - d;
  if (y <= 0) {
    cout << -1 << endl;
    return;
  }
  long long ans, res;
  if (x % y != 0) {
    res = x / y + 1;
  } else {
    res = x / y;
  }
  ans = b + res * c;
  cout << ans << endl;
}
int main() {
  int t;
  cin >> t;
  for (int i = 1; i <= t; ++i) {
    solve();
  }
}
