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