#include <bits/stdc++.h>
using namespace std;
int main() {
  long long unsigned t;
  cin >> t;
  while (t--) {
    long long unsigned a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a <= b) {
      cout << b << endl;
    } else if (a > b && c <= d) {
      cout << -1 << endl;
    } else if (a > b && c > d) {
      int more = a - b;
      int one = c - d;
      long long unsigned ans = (more + one - 1) / one;
      cout << ans * c + b << endl;
    }
  }
}
