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