#include <bits/stdc++.h>
using namespace std;
template <typename T, typename second>
ostream& operator<<(ostream& s, pair<T, second>& p) {
  s << '{' << p.first << ", " << p.second << '}';
  return s;
}
template <typename T>
ostream& operator<<(ostream& s, vector<T>& v) {
  if (v.empty()) {
    s << "[]";
    return s;
  }
  s << '[';
  for (int i = 0; i < v.size() - 1; i++) {
    s << v[i] << ", ";
  }
  s << v.back() << ']';
  return s;
}
int main() {
  int64_t a, b, c, d;
  int t;
  scanf("%d", &t);
  while (t--) {
    cin >> a >> b >> c >> d;
    int64_t ans = b;
    if (ans >= a) {
      cout << ans << endl;
      continue;
    }
    int64_t e = c - d;
    if (e <= 0) {
      cout << "-1" << endl;
      continue;
    }
    ans += ((a - b + e - 1) / e) * c;
    cout << ans << endl;
  }
  return 0;
}
