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