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