#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
int main() {
  int t;
  cin >> t;
  for (int i = 0; i < t; i++) {
    long long a, b, c, d;
    cin >> a >> b >> c >> d;
    if (a <= b) {
      cout << b << endl;
      continue;
    }
    if (a > b && c <= d) {
      cout << -1 << endl;
      continue;
    }
    if ((a - b) % (c - d) > 0)
      cout << b + ((a - b) / (c - d) + 1) * c << endl;
    else
      cout << b + ((a - b) / (c - d)) * c << endl;
  }
  return 0;
}
