#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  while (t--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << "\n";
      continue;
    }
    if (d >= c) {
      cout << -1 << "\n";
      continue;
    }
    long long t = (a - b) / (c - d);
    if ((a - b) % (c - d) != 0) t++;
    cout << b + t * c << "\n";
  }
}
