#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  long long tt;
  cin >> tt;
  while (tt--) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    a -= b;
    if (a <= 0) {
      cout << b << "\n";
    } else if (c <= d) {
      cout << -1 << "\n";
    } else {
      cout << (long long)(ceil((double)a / (c - d)) * c + b) << "\n";
    }
  }
  return 0;
}
