#include <bits/stdc++.h>
using namespace std;
const int N = 2e5 + 5;
long long t, a, b, c, d, n, m, k, x, y;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  ;
  cin >> t;
  while (t--) {
    cin >> a >> b >> c >> d;
    if (a <= b) {
      cout << b << endl;
      continue;
    }
    if (c <= d) {
      cout << -1 << endl;
      continue;
    }
    long long rem = a - b;
    long long div = rem / (c - d) + (rem % (c - d) != 0);
    long long ans = c * div;
    ans += b;
    cout << ans << endl;
  }
  return 0;
}
