#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long int t;
  cin >> t;
  while (t--) {
    long long int a, b, c, d;
    cin >> a >> b >> c >> d;
    if (b >= a) {
      cout << b << "\n";
      continue;
    } else {
      if (c <= d) {
        cout << "-1\n";
        continue;
      } else {
        long long int req = a - b;
        long long int ans = b;
        long long int cur = c - d;
        if (req % cur == 0) {
          ans += (req / cur) * c;
        } else {
          ans += ((req / cur) + 1) * c;
        }
        cout << ans << "\n";
      }
    }
  }
  return 0;
}
