#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long t, i, j, k, l, r, n, m, ans, sum, mx, mn, cnt;
  cin >> t;
  string s;
  while (t--) {
    cin >> i >> j >> l >> r;
    if (j >= i)
      cout << j << "\n";
    else if (l <= r)
      cout << "-1\n";
    else {
      i -= j;
      r = l - r;
      ans = j + (i + r - 1) / r * l;
      cout << ans << "\n";
    }
  }
  return 0;
}
