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