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