#include <bits/stdc++.h>
using namespace std;
int main() {
  int t;
  cin >> t;
  for (int p = 0; p < t; ++p) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int temp = c - d;
    if (b < a && c > d)
      cout << (long long)b + (long long)c * (((a - b) + temp - 1) / temp)
           << '\n';
    else if (b >= a)
      cout << b << '\n';
    else
      cout << -1 << '\n';
  }
  return 0;
}
