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