#include <bits/stdc++.h>
using namespace std;
int main() {
  int test;
  cin >> test;
  while (test--) {
    long long int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a <= b) {
      cout << b << endl;
    } else {
      long long int ans = b;
      long long int left = a - b;
      long long int diff = c - d;
      if (diff >= left) {
        ans = ans + c;
      } else {
        if (diff <= 0) {
          ans = -1;
        } else {
          if (left % diff == 0) {
            ans = ans + (left / diff) * c;
          } else {
            ans = ans + (left / diff + 1) * c;
          }
        }
      }
      cout << ans << endl;
    }
  }
}
