#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  int t;
  cin >> t;
  while (t--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << endl;
    } else {
      long long s = 0, time = 0;
      s = b, time = b;
      if (c <= d) {
        cout << -1 << endl;
      } else {
        long long i = 0;
        i = ceil((double)(a - s) / (double)(c - d));
        time += i * c;
        cout << time << endl;
      }
    }
  }
}
