#include <bits/stdc++.h>
using namespace std;
void solve() {
  long long int a, b, c, d;
  cin >> a >> b >> c >> d;
  if (a <= b) {
    cout << b << endl;
    return;
  }
  if (a > b && c <= d) {
    cout << "-1\n";
    return;
  }
  if ((a - b) % (c - d)) {
    cout << ((a - b) / (c - d) + 1) * c + b << endl;
    return;
  }
  cout << (a - b) / (c - d) * (c) + b << endl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int T = 1;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}
